Skip to content

Commit

Permalink
Fixed game detection in ScriptDatabaseEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
thesupersonic16 committed Apr 23, 2024
1 parent 5219b8c commit e61c796
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 102 deletions.
4 changes: 2 additions & 2 deletions DALTools/ScriptDatabaseEditor/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Game : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;

public GameLanguage GameLanguage { get; set; }
public bool EnableResourceLoading { get; set; }
public bool LoadResources { get; set; }

public string LangPath => Path.Combine(GamePath, "Data", GetLangDirectoryName(GameLanguage));
public string LangPathRel => Path.Combine("Data", GetLangDirectoryName(GameLanguage));
Expand All @@ -26,7 +26,7 @@ public Game(string path, GameLanguage lang)
{
GamePath = path;
GameLanguage = lang;
EnableResourceLoading = !string.IsNullOrEmpty(path);
LoadResources = Directory.Exists(LangPath);
}

public static string GetLangDirectoryName(GameLanguage lang)
Expand Down
62 changes: 29 additions & 33 deletions DALTools/ScriptDatabaseEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ public partial class MainWindow : Window, INotifyPropertyChanged

private TEXFile _optionTexture = new TEXFile();

private Game _game;

private Config _config = new Config();

public Config Config => _config;
public Game Game => _game;
public Game CurrentGame = null;
public Config CurrentConfig = new Config();

// Voice Names
public ImageSource VN_BG { get; set; }
Expand Down Expand Up @@ -71,17 +67,17 @@ public MainWindow()
public void LoadGameFiles()
{
// Check if files should not be loaded
if (!_game.EnableResourceLoading)
if (!CurrentGame.LoadResources)
return;
try
{
// Load Archives and Textures
_nameTextureArchive.Load(Path.Combine(_game.LangPath, "Event\\Name.pck"), true);
_movieTextureArchive.Load(Path.Combine(_game.LangPath, "Extra\\mov\\movieThumb.pck"), true);
_CGThumbnailTextureArchive.Load(Path.Combine(_game.GamePath, "Data\\Data\\Extra\\gal\\GalThumb.pck"), true);
_novelTextureArchive.Load(Path.Combine(_game.LangPath, "Extra\\nov\\NovData.pck"), true);
_novelthumbnailTextureArchive.Load(Path.Combine(_game.LangPath, "Extra\\nov\\NovThumb.pck"), true);
(_optionTexture = new TEXFile()).Load(Path.Combine(_game.LangPath, "Init\\option.tex"), true);
_nameTextureArchive.Load(Path.Combine(CurrentGame.LangPath, "Event\\Name.pck"), true);
_movieTextureArchive.Load(Path.Combine(CurrentGame.LangPath, "Extra\\mov\\movieThumb.pck"), true);
_CGThumbnailTextureArchive.Load(Path.Combine(CurrentGame.GamePath, "Data\\Data\\Extra\\gal\\GalThumb.pck"), true);
_novelTextureArchive.Load(Path.Combine(CurrentGame.LangPath, "Extra\\nov\\NovData.pck"), true);
_novelthumbnailTextureArchive.Load(Path.Combine(CurrentGame.LangPath, "Extra\\nov\\NovThumb.pck"), true);
(_optionTexture = new TEXFile()).Load(Path.Combine(CurrentGame.LangPath, "Init\\option.tex"), true);

// Create and Set ImageSource
VN_BG = ImageTools.ConvertToSource(_optionTexture.CreateBitmapFromFrame(27));
Expand All @@ -90,7 +86,7 @@ public void LoadGameFiles()
catch (Exception e)
{
// Disable Resources after failing the first time
_game.EnableResourceLoading = false;
CurrentGame.LoadResources = false;
// Show error
new ExceptionWindow(e).ShowDialog();
}
Expand All @@ -116,7 +112,7 @@ public void ChangeDatabase(string path)
/// <param name="lang">The Language to switch to</param>
public void SwitchLanguage(GameLanguage lang)
{
_game.GameLanguage = lang;
CurrentGame.GameLanguage = lang;
// Debug
App.Debug_GameLanguage = lang;
LoadGameFiles();
Expand All @@ -125,7 +121,7 @@ public void SwitchLanguage(GameLanguage lang)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Load Config from Registry
_config.LoadConfig();
CurrentConfig.LoadConfig();
List<SteamGame> games = new List<SteamGame>();
try
{
Expand All @@ -136,23 +132,23 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
games = Steam.SearchForGames("DATE A LIVE: Rio Reincarnation");
}catch { }

if (games.Count != 0)
_game = new Game(games[0].RootDirectory, GameLanguage.English);
else
_game = new Game("", GameLanguage.English);
//if (games.Count != 0)
// CurrentGame = new Game(games[0].RootDirectory, GameLanguage.English);
//else
CurrentGame = new Game("", GameLanguage.English);

// Debug
App.Debug_GamePath = _game?.GamePath;
App.Debug_GameLanguage = _config.DefaultGameLanguage;
App.Debug_GamePath = CurrentGame?.GamePath;
App.Debug_GameLanguage = CurrentConfig.DefaultGameLanguage;


// Set Default Language
_game.GameLanguage = _config.DefaultGameLanguage;
App.RunAnimations = _config.EnableAnimations;
CurrentGame.GameLanguage = CurrentConfig.DefaultGameLanguage;
App.RunAnimations = CurrentConfig.EnableAnimations;

// If not file was loaded, Load default
if (string.IsNullOrEmpty(App.DataBasePath))
App.DataBasePath = Path.Combine(_game.LangPath, @"Script\database.bin");
App.DataBasePath = Path.Combine(CurrentGame.LangPath, @"Script\database.bin");

if (File.Exists(App.DataBasePath))
ChangeDatabase(App.DataBasePath);
Expand All @@ -172,7 +168,7 @@ private void MoviesListView_DoubleClick(object sender, MouseButtonEventArgs e)
var list = sender as ListView;
if (list.SelectedIndex == -1)
return; // Return if no item is selected
new PropertyEditorMovie(_stscDatabase.Movies[list.SelectedIndex], _game, _movieTextureArchive).ShowDialog();
new PropertyEditorMovie(_stscDatabase.Movies[list.SelectedIndex], CurrentGame, _movieTextureArchive).ShowDialog();
}

private void ST_ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
Expand All @@ -198,7 +194,7 @@ private void VN_ListView_SelectionChanged(object sender, SelectionChangedEventAr
return; // Return if no item is selected

// Check if files should not be loaded
if (!_game.EnableResourceLoading)
if (!CurrentGame.LoadResources)
return;

int id = (list.SelectedItem as STSCFileDatabase.VoiceEntry).ID;
Expand All @@ -218,12 +214,12 @@ private void CG_ListView_SelectionChanged(object sender, SelectionChangedEventAr
return; // Return if no item is selected

// Check if files should not be loaded
if (!_game.EnableResourceLoading)
if (!CurrentGame.LoadResources)
return;

int id = (list.SelectedItem as STSCFileDatabase.CGEntry).CGID;
var game = (list.SelectedItem as STSCFileDatabase.CGEntry).GameID;
string filepath = Path.Combine(_game.GamePath, $"Data\\Data\\Ma\\{Consts.GAMEDIRNAME[(int)game]}\\MA{id:D6}.pck");
string filepath = Path.Combine(CurrentGame.GamePath, $"Data\\Data\\Ma\\{Consts.GAMEDIRNAME[(int)game]}\\MA{id:D6}.pck");

if (_CGThumbnailTextureArchive.GetFileData($"{Consts.GAMEDIRNAME[(int)game]}/MA{id:D6}.tex") is byte[] data)
{
Expand All @@ -239,7 +235,7 @@ private void CG_ListView_SelectionChanged(object sender, SelectionChangedEventAr
private void CG_ExportThumbButton_Click(object sender, RoutedEventArgs e)
{
// Show error if DAL: RR is not installed as its needed to export
if (!_game.EnableResourceLoading)
if (!CurrentGame.LoadResources)
{
MessageBox.Show("This feature requires DATE A LIVE: Rio Reincarnation to be installed!", "DAL: RR not found!", MessageBoxButton.OK, MessageBoxImage.Error);
return;
Expand Down Expand Up @@ -275,7 +271,7 @@ private void AB_SelectionChanged(object sender, SelectionChangedEventArgs e)
return; // Return if no item is selected

// Check if files should not be loaded
if (!_game.EnableResourceLoading)
if (!CurrentGame.LoadResources)
return;

string path = (list.SelectedItem as STSCFileDatabase.ArtBookPageEntry).PagePathData;
Expand Down Expand Up @@ -362,7 +358,7 @@ private void AB_SortPagesButton_Click(object sender, RoutedEventArgs e)
private void CG_ExportImage_Click(object sender, RoutedEventArgs e)
{
// Show error if DAL: RR is not installed as its needed to export
if (!_game.EnableResourceLoading)
if (!CurrentGame.LoadResources)
{
MessageBox.Show("Resource loading is currently disabled. Make sure DAL: RR is installed correctly", "Resource Loading Error!", MessageBoxButton.OK, MessageBoxImage.Error);
return;
Expand All @@ -380,7 +376,7 @@ private void CG_ExportImage_Click(object sender, RoutedEventArgs e)
{
try
{
string filepath = Path.Combine(_game.GamePath, $"Data\\Data\\Ma\\{Consts.GAMEDIRNAME[(int)game]}\\MA{id:D6}.pck");
string filepath = Path.Combine(CurrentGame.GamePath, $"Data\\Data\\Ma\\{Consts.GAMEDIRNAME[(int)game]}\\MA{id:D6}.pck");

var pck = new PCKFile();
var ma = new MAFile();
Expand Down
134 changes: 68 additions & 66 deletions DALTools/ScriptDatabaseEditor/Steam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,20 @@ public static List<SteamGame> SearchForGames(string preference = null)
paths.Add(Path.Combine(SteamLocation, "steamapps\\common"));

// Adds all the custom libraries
foreach (var library in SteamVDF.GetContainer(vdf, "LibraryFolders"))
if (int.TryParse(library.Key, out int index))
paths.Add(Path.Combine(library.Value as string, "steamapps\\common"));
var container = SteamVDF.GetContainer(vdf, "LibraryFolders");
if (container != null)
{
foreach (var library in container)
{
if (int.TryParse(library.Key, out int index))
{
if (library.Value is Dictionary<string, object> libraryInfo)
paths.Add(Path.Combine(libraryInfo["path"] as string ?? string.Empty, "steamapps\\common"));
else
paths.Add(Path.Combine(library.Value as string ?? string.Empty, "steamapps\\common"));
}
}
}

foreach (string path in paths)
{
Expand Down Expand Up @@ -93,7 +104,7 @@ public static Dictionary<string, object> GetContainer(
{
foreach (var value in containers)
{
if (value.Key == name)
if (string.Compare(value.Key, name, StringComparison.InvariantCultureIgnoreCase) == 0)
{
return value.Value as Dictionary<string, object>;
}
Expand All @@ -113,87 +124,78 @@ public static Dictionary<string, object> Load(string filePath)
public static Dictionary<string, object> Load(Stream fileStream)
{
var defs = new Dictionary<string, object>();
using (var reader = new StreamReader(fileStream, true))
{
string line, str = "", nm = "";
bool doReadString = false;
char c;
var reader = new StreamReader(fileStream, true);

ReadContainers(defs);
return defs;
string line, str = "", nm = "";
bool doReadString = false;
char c;

// Sub-Methods
void ReadContainers(Dictionary<string, object> parent)
return ReadContainers() ?? new Dictionary<string, object>();

// Sub-Methods
Dictionary<string, object> ReadContainers()
{
List<Dictionary<string, object>> containers = new List<Dictionary<string, object>>();
containers.Add(new Dictionary<string, object>());
string name = "";
nm = str = "";

while (!reader.EndOfStream)
{
Dictionary<string, object> container = null;
string name = "";
nm = str = "";
line = reader.ReadLine();
doReadString = false;

while (!reader.EndOfStream)
for (int i = 0; i < line.Length; ++i)
{
line = reader.ReadLine();
doReadString = false;

for (int i = 0; i < line.Length; ++i)
c = line[i];
if (c == '"')
{
c = line[i];
if (c == '"')
{
doReadString = !doReadString;
doReadString = !doReadString;

if (doReadString)
{
continue;
}
else
{
if (string.IsNullOrEmpty(nm))
{
nm = str;
str = "";
}
else
{
if (container != null)
container.Add(nm, str);
else
parent.Add(nm, str);

nm = str = "";
}
}
if (doReadString)
{
continue;
}
else if (c == '{')
else
{
if (container == null)
if (string.IsNullOrEmpty(nm))
{
container = new Dictionary<string, object>();
name = nm;
nm = str;
str = "";
}
else
{
var subContainer = new Dictionary<string, object>();
ReadContainers(subContainer);
container.Add(nm, subContainer);
}

nm = "";
}
else if (c == '}')
{
if (container != null)
{
parent.Add(name, container);
container = null;
if (containers.Count != 0)
containers.Last().Add(nm, str);
nm = str = "";
}
}
else if (doReadString)
}
else if (c == '{')
{
var container = new Dictionary<string, object>();
containers.Last().Add(nm, container);
containers.Add(container);
name = nm;
nm = "";
}
else if (c == '}')
{
if (containers.Count != 0)
{
str += c;
var container = containers.Last();
containers.Remove(container);
}
else
throw new Exception("Invalid VDF format!");
}
else if (doReadString)
{
str += c;
}
}
}
return containers.FirstOrDefault();
}
}
}
Expand All @@ -205,7 +207,7 @@ public class SteamGame
public string ExeName { get; set; }
public string RootDirectory { get; set; }
public string ExeDirectory { get { return Path.Combine(RootDirectory, ExeName); } }

public SteamGame(string gameName, string exe, string gameID)
{
GameName = gameName;
Expand Down
2 changes: 1 addition & 1 deletion DALTools/ScriptDatabaseEditor/UI/OptionsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public OptionsWindow(MainWindow window)

private void ButtonClose_Click(object sender, RoutedEventArgs e)
{
MainWindow.Config.SaveConfig();
MainWindow.CurrentConfig.SaveConfig();
Close();
}

Expand Down

0 comments on commit e61c796

Please sign in to comment.