Skip to content

Commit

Permalink
potentially fixed windows cli
Browse files Browse the repository at this point in the history
- fixed exceptions on broken pipe
- moved GetCharName to DataReader
  • Loading branch information
tolik518 committed Oct 24, 2023
1 parent 8682ecc commit 70a466f
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 110 deletions.
5 changes: 3 additions & 2 deletions SoG_SGreader/ComandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ namespace SoG_SGreader
{
class ComandLineOptions
{
[Value(0, MetaName = "savegame path", Required = false, HelpText = "Path to the savegame")]
[Value(0, MetaName = "savegame path", HelpText = "Path to the savegame", Required = false)]
public string SavegamePath { get; set; }

[Option('t', "text", HelpText = "Show a short summary of the savegame")]
public bool ShowText { get; set; }

[Option('j', "json", HelpText = "Print json of the savegame to console")]
//specify the json option and the path to the savegame
[Option('j', "json", HelpText = "Show the savegame as json")]
public bool ShowJson { get; set; }

[Option('h', "help", HelpText = "Show help")]
Expand Down
67 changes: 65 additions & 2 deletions SoG_SGreader/DataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SoG_SGreader
{
public class DataReader
public static class DataReader
{
public static Player ReadFromFile(string fileName, ITextBoxWrapper txtConsole)
{
Expand All @@ -16,7 +16,9 @@ public static Player ReadFromFile(string fileName, ITextBoxWrapper txtConsole)

txtConsole.AppendText("\r\nFilesize: " + new FileInfo(fileName).Length);

playerObject.MagicByte = readBinary.ReadInt32(); //I dont know yet what the first bytes stand for tbh
playerObject.MagicByte = readBinary.ReadInt32(); //I dont know yet what the first bytes stand for tbh

//TODO: Cast to SogItems
playerObject.Equip.Hat = readBinary.ReadInt32();
playerObject.Equip.Facegear = readBinary.ReadInt32();
playerObject.Style.Bodytype = readBinary.ReadChar(); //seems like always B ?
Expand All @@ -35,6 +37,7 @@ public static Player ReadFromFile(string fileName, ITextBoxWrapper txtConsole)
playerObject.Style.HatHidden = readBinary.ReadBoolean(); //[Sog_PlayerProperty(16)]
playerObject.Style.FacegearHidden = readBinary.ReadBoolean();

//TODO: Cast to SogItems
playerObject.LastTwoHander = readBinary.ReadInt32(); //last equipped onehander?
playerObject.LastOneHander = readBinary.ReadInt32(); //last equipped twohander?
playerObject.LastBow = readBinary.ReadInt32(); //last equipped bow?
Expand Down Expand Up @@ -370,5 +373,65 @@ public static Player ReadFromFile(string fileName, ITextBoxWrapper txtConsole)

return playerObject;
}

public static string GetCharName(string fileName)
{
using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
{
BinaryReader readBinary = new BinaryReader(fileStream);

readBinary.ReadInt32(); //I dont know yet what the first bytes stand for tbh
readBinary.ReadInt32(); //pPlayer.equip.Hat =
readBinary.ReadInt32(); //pPlayer.equip.Facegear =
readBinary.ReadChar(); // pPlayer.style.Bodytype =
readBinary.ReadInt32(); //pPlayer.style.Hair =
readBinary.ReadInt32(); //pPlayer.equip.Weapon =
readBinary.ReadInt32(); //pPlayer.equip.Shield =
readBinary.ReadInt32(); //pPlayer.equip.Armor =
readBinary.ReadInt32(); //pPlayer.equip.Shoes =
readBinary.ReadInt32(); //pPlayer.equip.Accessory1 =
readBinary.ReadInt32(); //pPlayer.equip.Accessory2 =
readBinary.ReadInt32(); //pPlayer.style.Hat =
readBinary.ReadInt32(); //pPlayer.style.Facegear =
readBinary.ReadInt32(); //pPlayer.style.Weapon =
readBinary.ReadInt32(); //pPlayer.style.Shield =

readBinary.ReadBoolean(); //pPlayer.style.HatHidden =
readBinary.ReadBoolean(); //pPlayer.style.FacegearHidden =

readBinary.ReadInt32(); //pPlayer.LastTwoHander =
readBinary.ReadInt32(); //pPlayer.LastOneHander =
readBinary.ReadInt32(); //pPlayer.LastBow =

for (int i = 0; i < 10; i++)
{
int quckslotType = readBinary.ReadByte();
if (quckslotType == 1)
{
readBinary.ReadInt32(); //pPlayer.quickslots.Add((Sog_Items)
}
else if (quckslotType == 2)
{
readBinary.ReadUInt16(); //pPlayer.quickslots.Add((Sog_Spells)
}
else
{

}
}

readBinary.ReadByte(); //pPlayer.style.HairColor =
readBinary.ReadByte(); //pPlayer.style.SkinColor =
readBinary.ReadByte(); //pPlayer.style.PonchoColor =
readBinary.ReadByte(); // pPlayer.style.ShirtColor =
readBinary.ReadByte(); //pPlayer.style.PantsColor =

readBinary.ReadByte(); //pPlayer.style.Sex =
string nickname = readBinary.ReadString();
readBinary.Dispose();

return nickname;
}
}
}
}
65 changes: 2 additions & 63 deletions SoG_SGreader/Forms/FrmLoadSaveGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,68 +28,7 @@ private void BtnLoadSaveGame_Click(object sender, EventArgs e)
frmMain.ShowDialog();
this.Close();
}

private string GetCharName(string fileName) // shouldnt be in that class here
{
using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
{

BinaryReader readBinary = new BinaryReader(fileStream);

readBinary.ReadInt32(); //I dont know yet what the first bytes stand for tbh
readBinary.ReadInt32(); //pPlayer.equip.Hat =
readBinary.ReadInt32(); //pPlayer.equip.Facegear =
readBinary.ReadChar(); // pPlayer.style.Bodytype =
readBinary.ReadInt32(); //pPlayer.style.Hair =
readBinary.ReadInt32(); //pPlayer.equip.Weapon =
readBinary.ReadInt32(); //pPlayer.equip.Shield =
readBinary.ReadInt32(); //pPlayer.equip.Armor =
readBinary.ReadInt32(); //pPlayer.equip.Shoes =
readBinary.ReadInt32(); //pPlayer.equip.Accessory1 =
readBinary.ReadInt32(); //pPlayer.equip.Accessory2 =
readBinary.ReadInt32(); //pPlayer.style.Hat =
readBinary.ReadInt32(); //pPlayer.style.Facegear =
readBinary.ReadInt32(); //pPlayer.style.Weapon =
readBinary.ReadInt32(); //pPlayer.style.Shield =

readBinary.ReadBoolean(); //pPlayer.style.HatHidden =
readBinary.ReadBoolean(); //pPlayer.style.FacegearHidden =

readBinary.ReadInt32(); //pPlayer.LastTwoHander =
readBinary.ReadInt32(); //pPlayer.LastOneHander =
readBinary.ReadInt32(); //pPlayer.LastBow =

for (int i = 0; i < 10; i++)
{
int quckslotType = readBinary.ReadByte();
if (quckslotType == 1)
{
readBinary.ReadInt32(); //pPlayer.quickslots.Add((Sog_Items)
}
else if (quckslotType == 2)
{
readBinary.ReadUInt16(); //pPlayer.quickslots.Add((Sog_Spells)
}
else
{

}
}

readBinary.ReadByte(); //pPlayer.style.HairColor =
readBinary.ReadByte(); //pPlayer.style.SkinColor =
readBinary.ReadByte(); //pPlayer.style.PonchoColor =
readBinary.ReadByte(); // pPlayer.style.ShirtColor =
readBinary.ReadByte(); //pPlayer.style.PantsColor =

readBinary.ReadByte(); //pPlayer.style.Sex =
string nickname = readBinary.ReadString();
readBinary.Dispose();

return nickname;
}
}


private void GetSaveGameFiles(string sFilePath)
{
lstvSaveGames.Items.Clear();
Expand All @@ -98,7 +37,7 @@ private void GetSaveGameFiles(string sFilePath)
string savegame = Path.Combine(sFilePath, i + ".cha");
if (File.Exists(savegame))
{
string[] item = new string[] { i + ".cha", GetCharName(savegame)};
string[] item = new string[] { i + ".cha", DataReader.GetCharName(savegame)};
lstvSaveGames.Items.Add(new ListViewItem(item, 0));
}
}
Expand Down
47 changes: 20 additions & 27 deletions SoG_SGreader/GamePatchReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

namespace SoG_SGreader
{
class GamePatchReader
static class GamePatchReader
{
public static async Task<string> GetCurrentGamePatchAsync(ITextBoxWrapper txtConsole)
{
string gameDataPath = GetGameDataPath(txtConsole);
if (string.IsNullOrEmpty(gameDataPath))
if (string.IsNullOrEmpty(gameDataPath) || !Directory.Exists(gameDataPath))
{
txtConsole.AppendText("\r\nGame data folder not found under: " + gameDataPath);
return "?.???a";
}

txtConsole.AppendText("\r\nGame data folder located: " + gameDataPath);

string exeFile = Path.Combine(gameDataPath, "Secrets Of Grindea.exe");

if (!File.Exists(exeFile))
Expand All @@ -33,30 +34,24 @@ private static string GetGameDataPath(ITextBoxWrapper txtConsole)
{
PlatformID pid = Environment.OSVersion.Platform;

if (pid == PlatformID.Win32NT || pid == PlatformID.Win32S || pid == PlatformID.Win32Windows || pid == PlatformID.WinCE)
switch (pid)
{
// Windows-specific code
txtConsole.AppendText("\r\nOS: Windows");
string steamPath = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Valve\Steam", "SteamPath", null);
return Path.Combine(steamPath, "steamapps", "common", "SecretsOfGrindea");
}
else if (pid == PlatformID.Unix)
{
// Linux-specific code
txtConsole.AppendText("\r\nOS: Linux");
return Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".local", "share", "Steam", "steamapps", "common", "SecretsOfGrindea");
}
else if (pid == PlatformID.MacOSX)
{
// MacOSX-specific code
txtConsole.AppendText("\r\nOS: MacOSX");
return Path.Combine(Environment.GetEnvironmentVariable("HOME"), "Library", "Application Support", "Steam", "steamapps", "common", "SecretsOfGrindeaa");
}
else
{
// Unknown OS
txtConsole.AppendText("\r\nOS: Unknown");
return null;
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
txtConsole.AppendText("\r\nOS: Windows");
string steamPath = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Valve\Steam", "SteamPath", null);
return Path.Combine(steamPath, "steamapps", "common", "SecretsOfGrindea");
case PlatformID.Unix:
txtConsole.AppendText("\r\nOS: Linux");
return Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".local", "share", "Steam", "steamapps", "common", "SecretsOfGrindea");
case PlatformID.MacOSX:
txtConsole.AppendText("\r\nOS: MacOSX");
return Path.Combine(Environment.GetEnvironmentVariable("HOME"), "Library", "Application Support", "Steam", "steamapps", "common", "SecretsOfGrindeaa");
default:
txtConsole.AppendText("\r\nOS: Unknown");
return null;
}
}

Expand Down Expand Up @@ -112,7 +107,5 @@ private static string GetLatestGamePatch(string exeFilePath, ITextBoxWrapper txt
}
return "?.???a";
}


}
}
31 changes: 23 additions & 8 deletions SoG_SGreader/JsonHandler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace SoG_SGreader
{
public class JsonHandler
public static class JsonHandler
{
private static bool IsDirectory(string jsonFilePath)
{
Expand All @@ -17,12 +18,14 @@ private static bool IsDirectory(string jsonFilePath)
return false;
}
}

public static string SaveGameToJson(Player player, string jsonFilePath)
{
JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Include;
serializer.Formatting = Formatting.Indented;

serializer.Converters.Add(new StringEnumConverter());

string jsonPath = jsonFilePath;

if (IsDirectory(jsonFilePath))
Expand Down Expand Up @@ -52,14 +55,26 @@ public static string GetJson(Player player)
JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Include;
serializer.Formatting = Formatting.Indented;

StringWriter sw = new StringWriter();
using (JsonWriter writer = new JsonTextWriter(sw))
serializer.Converters.Add(new StringEnumConverter()); // TODO: make this optional
try
{
serializer.Serialize(writer, player);
}
StringWriter sw = new StringWriter();
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, player);
}
return sw.ToString();

return sw.ToString();
}
catch (Exception e)
{
StringWriter sw = new StringWriter();
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, e);
}
return sw.ToString();
}
}
}
}
Loading

0 comments on commit 70a466f

Please sign in to comment.