558 lines
18 KiB
C#
Raw Normal View History

// WindowsPhoneSpeedyBlupi, Version=1.0.0.5, Culture=neutral, PublicKeyToken=6db12cd62dbec439
// WindowsPhoneSpeedyBlupi.Worlds
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
2025-01-14 15:34:06 +01:00
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using WindowsPhoneSpeedyBlupi;
2025-01-14 16:41:42 +01:00
#if KNI && Web
using Microsoft.JSInterop;
2025-01-14 15:34:06 +01:00
using Microsoft.AspNetCore.Components;
using static System.Runtime.InteropServices.JavaScript.JSType;
2025-01-14 16:41:42 +01:00
#endif
namespace WindowsPhoneSpeedyBlupi
{
public static class Worlds
{
private static StringBuilder output = new StringBuilder();
private static string GameDataFilename
{
get
{
return "SpeedyBlupi";
}
}
private static string CurrentGameFilename
{
get
{
return "CurrentGame";
}
}
public static string[] ReadWorld(int gamer, int rank)
{
2025-01-14 15:34:06 +01:00
string worldFilename = GetWorldFilename(gamer, rank);
2025-01-14 15:34:06 +01:00
string text = null;
2025-01-14 16:41:42 +01:00
#if !(KNI && Web)
try
{
2025-01-14 16:41:42 +01:00
Stream stream = TitleContainer.OpenStream(worldFilename);
StreamReader streamReader = new StreamReader(stream);
text = streamReader.ReadToEnd();
stream.Close();
}
2025-01-14 16:41:42 +01:00
catch (Exception e)
{
2025-01-14 16:41:42 +01:00
Debug.Write(e.Message);
Debug.Write("Fatal error. Loading world failed: " + worldFilename + "\n");
//Environment.Exit(1);
}
2025-01-14 16:41:42 +01:00
#else
text = WorldTxt.getWorld(rank);
#endif
if (text == null)
{
return null;
}
2025-01-14 18:36:10 +01:00
return text.Split('\n');
}
private static string GetWorldFilename(int gamer, int rank)
{
return string.Format("worlds/world{0}.txt", rank.ToString("d3"));
}
public static bool ReadGameData(byte[] data)
{
2025-01-14 15:34:06 +01:00
Debug.WriteLine("ReadGameData");
#if !(KNI && WEB)
2025-01-14 18:36:10 +01:00
IsolatedStorageFile userStoreForApplication = getUserStoreForApplication();
if (userStoreForApplication.FileExists(GameDataFilename))
{
IsolatedStorageFileStream isolatedStorageFileStream = null;
try
{
isolatedStorageFileStream = userStoreForApplication.OpenFile(GameDataFilename, FileMode.Open);
}
catch (IsolatedStorageException)
{
return false;
}
if (isolatedStorageFileStream != null)
{
int count = Math.Min(data.Length, (int)isolatedStorageFileStream.Length);
isolatedStorageFileStream.Read(data, 0, count);
isolatedStorageFileStream.Close();
return true;
}
}
return false;
2025-01-14 15:34:06 +01:00
#else
if (true) return false;
string result = LocalStorageHelperHolder.LocalStorageHelper.ReadFromLocalStorageAsync(GameDataFilename).Result;
if (result == null || result.Length == 0) { return false; }
else
{
byte[] resultAsByteArray = Encoding.UTF8.GetBytes(result);
Array.Copy(resultAsByteArray, data, resultAsByteArray.Length);
return true;
}
#endif
}
public static void WriteGameData(byte[] data)
{
2025-01-14 15:34:06 +01:00
Debug.WriteLine("WriteGameData");
#if !(KNI && WEB)
2025-01-14 18:36:10 +01:00
IsolatedStorageFile userStoreForApplication = getUserStoreForApplication();
IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.OpenFile(GameDataFilename, FileMode.Create);
if (isolatedStorageFileStream != null)
{
isolatedStorageFileStream.Write(data, 0, data.Length);
isolatedStorageFileStream.Close();
}
2025-01-14 15:34:06 +01:00
#else
//LocalStorageHelperHolder.LocalStorageHelper.SaveToLocalStorageAsync(GameDataFilename, Encoding.UTF8.GetString(data));
#endif
}
public static void DeleteCurrentGame()
{
2025-01-14 15:34:06 +01:00
Debug.WriteLine("DeleteCurrentGame");
#if !KNI
2025-01-14 18:36:10 +01:00
IsolatedStorageFile userStoreForApplication = getUserStoreForApplication();
try
{
userStoreForApplication.DeleteFile(CurrentGameFilename);
}
catch
{
}
2025-01-14 15:34:06 +01:00
#else
//LocalStorageHelperHolder.LocalStorageHelper.DeleteFromLocalStorageAsync(GameDataFilename);
#endif
}
public static string ReadCurrentGame()
{
2025-01-14 15:34:06 +01:00
Debug.WriteLine("ReadCurrentGame");
#if !(KNI && WEB)
2025-01-14 18:36:10 +01:00
IsolatedStorageFile userStoreForApplication = getUserStoreForApplication();
if (userStoreForApplication.FileExists(CurrentGameFilename))
{
IsolatedStorageFileStream isolatedStorageFileStream = null;
try
{
isolatedStorageFileStream = userStoreForApplication.OpenFile(CurrentGameFilename, FileMode.Open);
}
catch (IsolatedStorageException)
{
return null;
}
if (isolatedStorageFileStream != null)
{
byte[] array = new byte[isolatedStorageFileStream.Length];
isolatedStorageFileStream.Read(array, 0, array.Length);
isolatedStorageFileStream.Close();
return Encoding.UTF8.GetString(array, 0, array.Length);
}
}
return null;
2025-01-14 15:34:06 +01:00
#else
if (true) return null;
return LocalStorageHelperHolder.LocalStorageHelper.ReadFromLocalStorageAsync(CurrentGameFilename).Result;
#endif
}
public static void WriteCurrentGame(string data)
{
2025-01-14 15:34:06 +01:00
Debug.WriteLine("WriteCurrentGame");
#if !(KNI && WEB)
2025-01-14 18:36:10 +01:00
IsolatedStorageFile userStoreForApplication = getUserStoreForApplication();
IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.OpenFile(CurrentGameFilename, FileMode.Create);
if (isolatedStorageFileStream != null)
{
isolatedStorageFileStream.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
isolatedStorageFileStream.Close();
}
2025-01-14 15:34:06 +01:00
#else
//LocalStorageHelperHolder.LocalStorageHelper.SaveToLocalStorageAsync(CurrentGameFilename, data);
#endif
}
2025-01-14 18:36:10 +01:00
private static IsolatedStorageFile getUserStoreForApplication() {
if(Env.IMPL.isFNA()) {
return IsolatedStorageFile.GetUserStoreForAssembly();
}
return IsolatedStorageFile.GetUserStoreForApplication();
}
public static void GetIntArrayField(string[] lines, string section, int rank, string name, int[] array)
{
foreach (string text in lines)
{
if (!text.StartsWith(section + ":") || rank-- != 0)
{
continue;
}
int num = text.IndexOf(name + "=");
if (num == -1)
{
break;
}
num += name.Length + 1;
int num2 = text.IndexOf(" ", num);
if (num2 == -1)
{
break;
}
string[] array2 = text.Substring(num, num2 - num).Split(',');
for (int j = 0; j < array2.Length; j++)
{
int result;
if (int.TryParse(array2[j], out result))
{
array[j] = result;
}
else
{
array[j] = 0;
}
}
}
}
public static bool GetBoolField(string[] lines, string section, int rank, string name)
{
foreach (string text in lines)
{
if (text.StartsWith(section + ":") && rank-- == 0)
{
int num = text.IndexOf(name + "=");
if (num == -1)
{
return false;
}
num += name.Length + 1;
int num2 = text.IndexOf(" ", num);
if (num2 == -1)
{
return false;
}
string value = text.Substring(num, num2 - num);
bool result;
if (bool.TryParse(value, out result))
{
return result;
}
return false;
}
}
return false;
}
public static int GetIntField(string[] lines, string section, int rank, string name)
{
foreach (string text in lines)
{
if (text.StartsWith(section + ":") && rank-- == 0)
{
int num = text.IndexOf(name + "=");
if (num == -1)
{
return 0;
}
num += name.Length + 1;
int num2 = text.IndexOf(" ", num);
if (num2 == -1)
{
return 0;
}
string s = text.Substring(num, num2 - num);
int result;
if (int.TryParse(s, out result))
{
return result;
}
return 0;
}
}
return 0;
}
public static double GetDoubleField(string[] lines, string section, int rank, string name)
{
foreach (string text in lines)
{
if (text.StartsWith(section + ":") && rank-- == 0)
{
int num = text.IndexOf(name + "=");
if (num == -1)
{
return 0.0;
}
num += name.Length + 1;
int num2 = text.IndexOf(" ", num);
if (num2 == -1)
{
return 0.0;
}
string s = text.Substring(num, num2 - num);
double result;
if (double.TryParse(s, out result))
{
return result;
}
return 0.0;
}
}
return 0.0;
}
public static TinyPoint GetPointField(string[] lines, string section, int rank, string name)
{
foreach (string text in lines)
{
if (text.StartsWith(section + ":") && rank-- == 0)
{
int num = text.IndexOf(name + "=");
if (num == -1)
{
return default;
}
num += name.Length + 1;
int num2 = text.IndexOf(";", num);
if (num2 == -1)
{
return default;
}
int num3 = text.IndexOf(" ", num);
if (num3 == -1)
{
return default;
}
string s = text.Substring(num, num2 - num);
string s2 = text.Substring(num2 + 1, num3 - num2 - 1);
int result;
if (!int.TryParse(s, out result))
{
return default;
}
int result2;
if (!int.TryParse(s2, out result2))
{
return default;
}
TinyPoint result3 = default;
result3.X = result;
result3.Y = result2;
return result3;
}
}
return default;
}
public static int? GetDecorField(string[] lines, string section, int x, int y)
{
for (int i = 0; i < lines.Length; i++)
{
string text = lines[i];
if (text.StartsWith(section + ":"))
{
text = lines[i + 1 + x];
string[] array = text.Split(',');
if (string.IsNullOrEmpty(array[y]))
{
return -1;
}
int result;
if (int.TryParse(array[y], out result))
{
return result;
}
return null;
}
}
return null;
}
public static void GetDoorsField(string[] lines, string section, int[] doors)
{
foreach (string text in lines)
{
if (!text.StartsWith(section + ":"))
{
continue;
}
string[] array = text.Substring(section.Length + 2).Split(',');
for (int j = 0; j < array.Length; j++)
{
int result;
if (string.IsNullOrEmpty(array[j]))
{
doors[j] = 1;
}
else if (int.TryParse(array[j], out result))
{
doors[j] = result;
}
}
}
}
public static void WriteClear()
{
output.Clear();
}
public static void WriteSection(string section)
{
output.Append(section);
output.Append(": ");
}
public static void WriteIntArrayField(string name, int[] array)
{
output.Append(name);
output.Append("=");
for (int i = 0; i < array.Length; i++)
{
if (array[i] != 0)
{
output.Append(array[i].ToString(CultureInfo.InvariantCulture));
}
if (i < array.Length - 1)
{
output.Append(",");
}
}
output.Append(" ");
}
public static void WriteBoolField(string name, bool n)
{
output.Append(name);
output.Append("=");
output.Append(n.ToString(CultureInfo.InvariantCulture));
output.Append(" ");
}
public static void WriteIntField(string name, int n)
{
output.Append(name);
output.Append("=");
output.Append(n.ToString(CultureInfo.InvariantCulture));
output.Append(" ");
}
public static void WriteDoubleField(string name, double n)
{
output.Append(name);
output.Append("=");
output.Append(n.ToString(CultureInfo.InvariantCulture));
output.Append(" ");
}
public static void WritePointField(string name, TinyPoint p)
{
output.Append(name);
output.Append("=");
output.Append(p.X.ToString(CultureInfo.InvariantCulture));
output.Append(";");
output.Append(p.Y.ToString(CultureInfo.InvariantCulture));
output.Append(" ");
}
public static void WriteDecorField(int[] line)
{
for (int i = 0; i < line.Length; i++)
{
if (line[i] != -1)
{
output.Append(line[i].ToString(CultureInfo.InvariantCulture));
}
if (i < line.Length - 1)
{
output.Append(",");
}
}
output.Append("\n");
}
public static void WriteDoorsField(int[] doors)
{
for (int i = 0; i < doors.Length; i++)
{
if (doors[i] != 1)
{
output.Append(doors[i].ToString(CultureInfo.InvariantCulture));
}
if (i < doors.Length - 1)
{
output.Append(",");
}
}
output.Append("\n");
}
public static void WriteEndSection()
{
output.Append("\n");
}
public static string GetWriteString()
{
return output.ToString();
}
2025-01-14 15:34:06 +01:00
}
#if KNI && WEB
public class LocalStorageHelper
{
private readonly IJSRuntime _jsRuntime;
// Constructor to inject IJSRuntime
public LocalStorageHelper(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}
public async Task SaveToLocalStorageAsync(string key, string data)
{
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", key, data);
}
public async Task<string> ReadFromLocalStorageAsync(string key)
{
return await _jsRuntime.InvokeAsync<string>("localStorage.getItem", key);
}
public async Task DeleteFromLocalStorageAsync(string key)
{
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", key);
}
}
2025-01-14 15:34:06 +01:00
#endif
}