Skip to content

Commit

Permalink
FrostyStringGeneration project initial
Browse files Browse the repository at this point in the history
  • Loading branch information
wavebend committed Jan 17, 2025
1 parent a1130a3 commit 079ec13
Show file tree
Hide file tree
Showing 40 changed files with 69,595 additions and 65 deletions.
2 changes: 1 addition & 1 deletion FrostyCmd/ShaderDbDump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public void Execute(string basePath, string filename, ILogger logger)
{
FileInfo baseFile = new FileInfo(basePath);

ProfilesLibrary.Initialize(baseFile.Name.Replace(baseFile.Extension, ""));
ProfilesLibrary.SelectProfile(baseFile.Name.Replace(baseFile.Extension, ""));
TypeLibrary.Initialize(false);

FileSystemManager fs = new FileSystemManager(baseFile.DirectoryName);
Expand Down
2 changes: 2 additions & 0 deletions FrostyEditor/ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ New Features:

Improvements:
- Experimental: significantly improved data explorer responsiveness
- Ebx: Added YAML export option
- Ebx: Added option to export debug offsets (Tools->Options)

Bug Fixes:
- Fixed some crashing issues due to missing assets when rewriting bundles
Expand Down
12 changes: 12 additions & 0 deletions FrostyEditor/FrostyEditor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FrostyCmd", "..\FrostyCmd\F
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnimationEditorPlugin", "..\Plugins\AnimationEditorPlugin\AnimationEditorPlugin.csproj", "{CE312C84-2677-433C-B4B1-352BD16E7590}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FrostyStringGeneration", "..\FrostyStringGeneration\FrostyStringGeneration.csproj", "{56980B5A-1E2E-43F9-86BA-D670CF413F9E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Developer - Debug|x64 = Developer - Debug|x64
Expand Down Expand Up @@ -524,6 +526,16 @@ Global
{CE312C84-2677-433C-B4B1-352BD16E7590}.Release - Final|x64.Build.0 = Release - Final|x64
{CE312C84-2677-433C-B4B1-352BD16E7590}.Release|x64.ActiveCfg = Developer - Debug|x64
{CE312C84-2677-433C-B4B1-352BD16E7590}.Release|x64.Build.0 = Developer - Debug|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Developer - Debug|x64.ActiveCfg = Developer - Debug|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Developer - Debug|x64.Build.0 = Developer - Debug|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Release - Alpha|x64.ActiveCfg = Release - Alpha|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Release - Alpha|x64.Build.0 = Release - Alpha|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Release - Beta|x64.ActiveCfg = Release - Beta|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Release - Beta|x64.Build.0 = Release - Beta|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Release - Final|x64.ActiveCfg = Release - Final|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Release - Final|x64.Build.0 = Release - Final|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Release|x64.ActiveCfg = Release - Final|x64
{56980B5A-1E2E-43F9-86BA-D670CF413F9E}.Release|x64.Build.0 = Release - Final|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 9 additions & 2 deletions FrostyEditor/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,15 @@ private void contextMenuRevert_Click(object sender, RoutedEventArgs e)
}

FrostyTaskWindow.Show("Reverting Asset", "", (task) => { App.AssetManager.RevertAsset(entry, suppressOnModify: false); });

ResetItemsSources();

if (entry.IsAdded)
{
ResetItemsSources();
}
else
{
dataExplorer.RefreshItems();
}
}

private void contextMenuImportAsset_Click(object sender, RoutedEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion FrostyEditor/Windows/PrelaunchWindow2.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public PrelaunchWindow2()
private void LaunchConfig(string profile)
{
// load profiles
if (!ProfilesLibrary.Initialize(profile))
if (!ProfilesLibrary.SelectProfile(profile))
{
FrostyMessageBox.Show("There was an error when trying to load game using specified profile.", "Frosty Editor");
Close();
Expand Down
2 changes: 1 addition & 1 deletion FrostyEditor/Windows/SplashWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ await Task.Run(() =>
private async Task<int> LoadStringList(ILogger logger)
{
logger.Log("Loading custom strings");
await Task.Run(() => Utils.GetString(0));
await Task.Run(() => StringsManager.GetString(0));
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion FrostyModManager/Windows/PrelaunchWindow2.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public PrelaunchWindow2()
private void LaunchConfig(string profile)
{
// load profiles
if (!ProfilesLibrary.Initialize(profile))
if (!ProfilesLibrary.SelectProfile(profile))
{
FrostyMessageBox.Show("There was an error when trying to load game using specified profile.", "Frosty Mod Manager");
Close();
Expand Down
2 changes: 1 addition & 1 deletion FrostyPlugin/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void ClearProfileData()
public static bool LoadProfile(string profile)
{
// load profiles
if (!ProfilesLibrary.Initialize(profile))
if (!ProfilesLibrary.SelectProfile(profile))
{
FrostyMessageBox.Show("There was an error when trying to load game using specified profile.", "Frosty Core");
return false;
Expand Down
6 changes: 5 additions & 1 deletion FrostyPlugin/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,11 @@ public static void Save(string path = "")
public static void Load(string path = "")
{
if (path == "")
path = App.PluginManager.ManagerType == PluginManagerType.Editor ? $"{App.GlobalSettingsPath}/editor_config.json" : $"{App.GlobalSettingsPath}/manager_config.json";
{
path = (App.PluginManager == null || App.PluginManager.ManagerType == PluginManagerType.Editor)
? $"{App.GlobalSettingsPath}/editor_config.json"
: $"{App.GlobalSettingsPath}/manager_config.json";
}

using (StreamReader reader = new StreamReader(path))
{
Expand Down
2 changes: 1 addition & 1 deletion FrostyPlugin/Windows/FrostyProfileTaskWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ await Task.Run(() =>
private async Task<int> LoadStringList()
{
TaskLogger.Log("Loading custom strings");
await Task.Run(() => Utils.LoadStringList("strings.txt", TaskLogger));
await Task.Run(() => StringsManager.LoadStringList("strings.txt", TaskLogger));
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions FrostySdk/FrostySdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
<Compile Include="Resources\Resource.cs" />
<Compile Include="Resources\Texture.cs" />
<Compile Include="Sha1.cs" />
<Compile Include="StringsManager.cs" />
<Compile Include="TypeLibrary.cs" />
<Compile Include="Utils.cs" />
<Compile Include="Utils\HuffmanDecoder.cs" />
Expand Down
28 changes: 22 additions & 6 deletions FrostySdk/ProfilesLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static bool IsResTypeIgnored(Managers.Entries.ResourceType resType)

private static readonly List<Profile> m_profiles = new List<Profile>();

public static void Initialize(IEnumerable<Profile> pluginProfiles)
public static void Initialize(IEnumerable<Profile> pluginProfiles = null)
{
List<string> keys = new List<string>();
List<long> offsets = new List<long>();
Expand Down Expand Up @@ -201,20 +201,36 @@ public static void Initialize(IEnumerable<Profile> pluginProfiles)
m_profiles.Add(profileStruct);
}
}

// Add profiles from plugins
foreach (Profile profile in pluginProfiles)
m_profiles.Add(profile);
if (pluginProfiles != null)
{
// Add profiles from plugins
foreach (Profile profile in pluginProfiles)
m_profiles.Add(profile);
}
}

public static bool Initialize(string profileKey)
public static bool SelectProfile(string profileKey)
{
Profile? profile = m_profiles.Find((Profile a) => a.Name.Equals(profileKey, StringComparison.OrdinalIgnoreCase));
m_effectiveProfile = profile.Value;

return true;
}

public static bool SelectProfile(ProfileVersion version)
{
Profile? profile = m_profiles.Find((Profile a) => (ProfileVersion)a.DataVersion == version);
m_effectiveProfile = profile.Value;

return true;
}

public static Profile? GetProfileDefinition(ProfileVersion version)
{
Profile? profile = m_profiles.Find((Profile a) => (ProfileVersion)a.DataVersion == version);
return profile;
}

public static bool HasProfile(string profileKey)
{
return m_profiles.FindIndex((Profile a) => a.Name.Equals(profileKey, StringComparison.OrdinalIgnoreCase)) != -1;
Expand Down
94 changes: 94 additions & 0 deletions FrostySdk/StringsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Frosty.Hash;
using FrostySdk.Interfaces;
using FrostySdk.IO;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Shapes;

namespace FrostySdk
{
public static class StringsManager
{
private static Dictionary<int, string> strings = new Dictionary<int, string>();

public static string GetString(int hash)
{
if (!strings.TryGetValue(hash, out var value))
{
return "0x" + hash.ToString("x8");
}
return value;
}

/// <summary>
/// Loads all resolved hashes
/// </summary>
/// <param name="path">The file to be read from for hashes.</param>
public static void LoadStringList(string path = "strings.txt", ILogger logger = null)
{
if (!File.Exists(path))
{
return;
}

strings.Clear();

try
{
var allLines = File.ReadAllLines(path);
int totalLines = allLines.Length;
int concurrencyLevel = Environment.ProcessorCount;
var partitioner = Partitioner.Create(0, totalLines, Math.Max(totalLines / concurrencyLevel, 1));
var localDictionaries = new ConcurrentBag<Dictionary<int, string>>();

int processedCount = 0;
int logIntervals = 20;
int logInterval = Math.Max(totalLines / logIntervals, 1);

Parallel.ForEach(partitioner, range =>
{
var localDict = new Dictionary<int, string>(range.Item2 - range.Item1);

for (int i = range.Item1; i < range.Item2; i++)
{
var currentString = allLines[i];
int hash = Fnv1.HashString(currentString);
localDict[hash] = currentString;
if (logger != null)
{
int countSoFar = Interlocked.Increment(ref processedCount);
if (countSoFar % logInterval == 0 || countSoFar == totalLines)
{
double progress = (double)countSoFar / totalLines * 100.0;
logger.Log($"progress: {progress}");
}
}
}

localDictionaries.Add(localDict);
});

var finalDictionary = new Dictionary<int, string>(totalLines);
foreach (var dict in localDictionaries)
{
foreach (var kvp in dict)
{
finalDictionary[kvp.Key] = kvp.Value;
}
}

strings = finalDictionary;
}
catch (Exception ex)
{
logger?.Log($"Error loading strings: {ex.Message}");
}
}
}
}
41 changes: 1 addition & 40 deletions FrostySdk/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -855,17 +855,7 @@ public static int HashString(string strToHash, bool lowercase = false)
return Fnv1.HashString(strToHash);
}

private static Dictionary<int, string> strings = new Dictionary<int, string>();

public static string GetString(int hash)
{
if (!strings.ContainsKey(hash))
{
return "0x" + hash.ToString("x8");
}

return strings[hash];
}
public static string GetString(int hash) => StringsManager.GetString(hash);

public static string ReverseString(string str)
{
Expand Down Expand Up @@ -1300,34 +1290,5 @@ private static unsafe uint CalcFletcher32Internal(byte[] array)

return (uint)((int)((part1 & 0xFFFF0000) + (part1 << 16)) | ((ushort)part2 + (part2 >> 16)));
}

/// <summary>
/// Loads all resolved hashes that are found within the specified file of <paramref name="path"/>.
/// </summary>
/// <param name="path">The file to be read from for hashes.</param>
public static void LoadStringList(string path = "strings.txt", ILogger logger = null)
{
if (!File.Exists(path))
{
return;
}

strings.Clear();

using (NativeReader reader = new NativeReader(new FileStream(path, FileMode.Open, FileAccess.Read)))
{
while (reader.Position < reader.Length)
{
string currentString = reader.ReadLine();
int hash = Fnv1.HashString(currentString);
if (!strings.ContainsKey(hash))
{
strings.Add(hash, currentString);
}

logger?.Log("progress:" + (double)reader.Position / (double)reader.Length * 100.0);
}
}
}
}
}
13 changes: 13 additions & 0 deletions FrostyStringGeneration/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[*.cs]

# CS1591: Missing XML comment for publicly visible type or member
dotnet_diagnostic.CS1591.severity = none

#place catch statements on a new line
csharp_new_line_before_catch = true
#place else statements on a new line
csharp_new_line_before_else = true
#require members of object intializers to be on separate lines
csharp_new_line_before_members_in_object_initializers = true
#require braces to be on a new line for methods, control_blocks, types, lambdas, and object_collection_array_initializers (also known as "Allman" style)
csharp_new_line_before_open_brace = methods, control_blocks, types, lambdas, object_collection_array_initializers
Loading

0 comments on commit 079ec13

Please sign in to comment.