Skip to content

Commit

Permalink
this is it luigi
Browse files Browse the repository at this point in the history
  • Loading branch information
N1ghtTheF0x committed Nov 10, 2024
1 parent 466810f commit 2d20d2a
Show file tree
Hide file tree
Showing 22 changed files with 650 additions and 48 deletions.
21 changes: 6 additions & 15 deletions TFHRES.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@
using ThemModdingHerds.TFHResource;
using ThemModdingHerds.TFHResource.Data;

new Database().Save("test.tfhres",true);

Database db = Database.Open("test.tfhres");
Database db2 = new();

db2.Insert(new CacheRecord()
{
Shortname = "a",
SourcePath = "b"
});

Database both = db.Merge(db2);
db.Close();
db2.Close();
both.Save("test.tfhres",true);
string path = "G:\\SteamLibrary\\steamapps\\common\\Them's Fightin' Herds\\Scripts\\src\\Farm\\resources\\resources_prod.tfhres";
Database database = Database.Open(path);
Database mod = Database.Open("G:\\ThemModdingHerds\\VelvetBeautifier\\CLI\\bin\\Debug\\net8.0\\mods\\ntf.tfh.halloweenlobbies\\resources_prod.tfhres");
Database result = database.Clone();
result.Upsert(mod);
result.Save("test.tfhres",true);
14 changes: 5 additions & 9 deletions TFHRES/Database.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.ComponentModel;
using System.Data;
using Microsoft.Data.Sqlite;
using ThemModdingHerds.TFHResource.Data;
namespace ThemModdingHerds.TFHResource;
public class Database : IDisposable, ICloneable
public class Database : IDisposable
{
public SqliteConnection Connection {get;private set;}
public ConnectionState State {get => Connection.State;}
Expand Down Expand Up @@ -32,13 +30,11 @@ public void Close()
{
Connection.Close();
}
public object Clone()
public Database Clone()
{
return new Database(Connection.Clone());
}
public Database Merge(params Database[] databases)
{
return DatabaseUtils.Merge([this,..databases]);
Database clone = new();
DatabaseUtils.ForceInsert(clone,this);
return clone;
}
public void Save(string path,bool overwrite = false)
{
Expand Down
92 changes: 89 additions & 3 deletions TFHRES/DatabaseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ public static void CreateTables(SqliteConnection connection)
command.ExecuteNonQuery();
}
}
public static Database Merge(IEnumerable<Database> databases)
public static void Upsert(this Database db,params Database[] databases)
{
Database db = new();
foreach(Database database in databases)
{
db.Upsert(database.ReadCacheRecord());
Expand All @@ -131,6 +130,93 @@ public static Database Merge(IEnumerable<Database> databases)
db.Upsert(database.ReadTmxMapInstanceLayersItemsItemAnimationsItems());
db.Upsert(database.ReadVarstate());
}
return db;
}
public static void Update(this Database db,params Database[] databases)
{
foreach(Database database in databases)
{
db.Update(database.ReadCacheRecord());
db.Update(database.ReadCachedImage());
db.Update(database.ReadCachedTextfile());
db.Update(database.ReadFilemapRecord());
db.Update(database.ReadImageBiomeRecord());
db.Update(database.ReadInkBytecode());
db.Update(database.ReadJotBytecode());
db.Update(database.ReadLocalizedText());
db.Update(database.ReadMapBiomeRecord());
db.Update(database.ReadPixelanim());
db.Update(database.ReadPrecacheRecord());
db.Update(database.ReadSwfanim());
db.Update(database.ReadTmxMapInstance());
db.Update(database.ReadTmxMapInstanceLayersItems());
db.Update(database.ReadTmxMapInstanceLayersItemsItemAnimationsItems());
db.Update(database.ReadVarstate());
}
}
public static void Insert(this Database db,params Database[] databases)
{
foreach(Database database in databases)
{
db.Insert(database.ReadCacheRecord());
db.Insert(database.ReadCachedImage());
db.Insert(database.ReadCachedTextfile());
db.Insert(database.ReadFilemapRecord());
db.Insert(database.ReadImageBiomeRecord());
db.Insert(database.ReadInkBytecode());
db.Insert(database.ReadJotBytecode());
db.Insert(database.ReadLocalizedText());
db.Insert(database.ReadMapBiomeRecord());
db.Insert(database.ReadPixelanim());
db.Insert(database.ReadPrecacheRecord());
db.Insert(database.ReadSwfanim());
db.Insert(database.ReadTmxMapInstance());
db.Insert(database.ReadTmxMapInstanceLayersItems());
db.Insert(database.ReadTmxMapInstanceLayersItemsItemAnimationsItems());
db.Insert(database.ReadVarstate());
}
}
public static void ForceInsert(this Database db,params Database[] databases)
{
foreach(Database database in databases)
{
db.ForceInsert(database.ReadCacheRecord());
db.ForceInsert(database.ReadCachedImage());
db.ForceInsert(database.ReadCachedTextfile());
db.ForceInsert(database.ReadFilemapRecord());
db.ForceInsert(database.ReadImageBiomeRecord());
db.ForceInsert(database.ReadInkBytecode());
db.ForceInsert(database.ReadJotBytecode());
db.ForceInsert(database.ReadLocalizedText());
db.ForceInsert(database.ReadMapBiomeRecord());
db.ForceInsert(database.ReadPixelanim());
db.ForceInsert(database.ReadPrecacheRecord());
db.ForceInsert(database.ReadSwfanim());
db.ForceInsert(database.ReadTmxMapInstance());
db.ForceInsert(database.ReadTmxMapInstanceLayersItems());
db.ForceInsert(database.ReadTmxMapInstanceLayersItemsItemAnimationsItems());
db.ForceInsert(database.ReadVarstate());
}
}
public static void Delsert(this Database db,params Database[] databases)
{
foreach(Database database in databases)
{
db.Delsert(database.ReadCacheRecord());
db.Delsert(database.ReadCachedImage());
db.Delsert(database.ReadCachedTextfile());
db.Delsert(database.ReadFilemapRecord());
db.Delsert(database.ReadImageBiomeRecord());
db.Delsert(database.ReadInkBytecode());
db.Delsert(database.ReadJotBytecode());
db.Delsert(database.ReadLocalizedText());
db.Delsert(database.ReadMapBiomeRecord());
db.Delsert(database.ReadPixelanim());
db.Delsert(database.ReadPrecacheRecord());
db.Delsert(database.ReadSwfanim());
db.Delsert(database.ReadTmxMapInstance());
db.Delsert(database.ReadTmxMapInstanceLayersItems());
db.Delsert(database.ReadTmxMapInstanceLayersItemsItemAnimationsItems());
db.Delsert(database.ReadVarstate());
}
}
}
2 changes: 1 addition & 1 deletion TFHRES/TFHRES.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>ThemModdingHerds.$(MSBuildProjectName)</AssemblyName>
<Version>1.3.6</Version>
<Version>1.4.0</Version>
<Authors>NightTheFox</Authors>
<Description>Them's Fightin' Herds .tfhres reader/writer library</Description>
<PackageProjectUrl>https://github.com/ThemModdingHerds</PackageProjectUrl>
Expand Down
30 changes: 29 additions & 1 deletion TFHRES/tables/CacheRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,28 @@ public static void Insert(this Database database,CacheRecord cache_record)
cmd.Parameters.AddWithValue("$source_path",cache_record.SourcePath);
cmd.ExecuteNonQuery();
}
public static void ForceInsert(this Database database,CacheRecord cache_record)
{
SqliteCommand cmd = database.Connection.CreateCommand();
cmd.CommandText = $"INSERT INTO {CacheRecord.TABLE_NAME} (hiberlite_id,shortname,source_path) VALUES ($hiberlite_id,$shortname,$source_path);";
cmd.Parameters.AddWithValue("$hiberlite_id",cache_record.HiberliteId);
cmd.Parameters.AddWithValue("$shortname",cache_record.Shortname);
cmd.Parameters.AddWithValue("$source_path",cache_record.SourcePath);
cmd.ExecuteNonQuery();
}
public static void Insert(this Database database,IEnumerable<CacheRecord> items)
{
foreach(CacheRecord cache_record in items)
Insert(database,cache_record);
}
public static void ForceInsert(this Database database,IEnumerable<CacheRecord> items)
{
foreach(CacheRecord cache_record in items)
ForceInsert(database,cache_record);
}
public static void Upsert(this Database database,CacheRecord cache_record)
{
if(ExistsCacheRecord(database,cache_record))
if(ExistsCacheRecord(database,cache_record.HiberliteId))
{
Update(database,cache_record);
return;
Expand All @@ -40,6 +54,20 @@ public static void Upsert(this Database database,IEnumerable<CacheRecord> items)
foreach(CacheRecord cache_record in items)
Upsert(database,cache_record);
}
public static void Delsert(this Database database,CacheRecord cache_record)
{
if(ExistsCacheRecord(database,cache_record.HiberliteId))
{
DeleteCacheRecord(database,cache_record.HiberliteId);
return;
}
ForceInsert(database,cache_record);
}
public static void Delsert(this Database database,IEnumerable<CacheRecord> items)
{
foreach(CacheRecord cache_record in items)
Delsert(database,cache_record);
}
public static void Update(this Database database,CacheRecord cache_record)
{
SqliteCommand cmd = database.Connection.CreateCommand();
Expand Down
34 changes: 33 additions & 1 deletion TFHRES/tables/CachedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,32 @@ public static void Insert(this Database database,CachedImage cached_image)
cmd.Parameters.AddWithValue("$width",cached_image.Width);
cmd.ExecuteNonQuery();
}
public static void ForceInsert(this Database database,CachedImage cached_image)
{
SqliteCommand cmd = database.Connection.CreateCommand();
cmd.CommandText = $"INSERT INTO {CachedImage.TABLE_NAME} (height,hiberlite_id,image_data,is_compressed,shortname,vram_only,width) VALUES ($height,$hiberlite_id,$image_data,$is_compressed,$shortname,$vram_only,$width);";
cmd.Parameters.AddWithValue("$height",cached_image.Height);
cmd.Parameters.AddWithValue("$hiberlite_id",cached_image.HiberliteId);
cmd.Parameters.AddWithValue("$image_data",cached_image.ImageData);
cmd.Parameters.AddWithValue("$is_compressed",cached_image.IsCompressed);
cmd.Parameters.AddWithValue("$shortname",cached_image.Shortname);
cmd.Parameters.AddWithValue("$vram_only",cached_image.VramOnly);
cmd.Parameters.AddWithValue("$width",cached_image.Width);
cmd.ExecuteNonQuery();
}
public static void Insert(this Database database,IEnumerable<CachedImage> items)
{
foreach(CachedImage cached_image in items)
Insert(database,cached_image);
}
public static void ForceInsert(this Database database,IEnumerable<CachedImage> items)
{
foreach(CachedImage cached_image in items)
ForceInsert(database,cached_image);
}
public static void Upsert(this Database database,CachedImage cached_image)
{
if(ExistsCachedImage(database,cached_image))
if(ExistsCachedImage(database,cached_image.HiberliteId))
{
Update(database,cached_image);
return;
Expand All @@ -48,6 +66,20 @@ public static void Upsert(this Database database,IEnumerable<CachedImage> items)
foreach(CachedImage cached_image in items)
Upsert(database,cached_image);
}
public static void Delsert(this Database database,CachedImage cached_image)
{
if(ExistsCachedImage(database,cached_image.HiberliteId))
{
DeleteCachedImage(database,cached_image.HiberliteId);
return;
}
ForceInsert(database,cached_image);
}
public static void Delsert(this Database database,IEnumerable<CachedImage> items)
{
foreach(CachedImage cached_image in items)
Delsert(database,cached_image);
}
public static void Update(this Database database,CachedImage cached_image)
{
SqliteCommand cmd = database.Connection.CreateCommand();
Expand Down
31 changes: 30 additions & 1 deletion TFHRES/tables/CachedTextfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,29 @@ public static void Insert(this Database database,CachedTextfile cached_textfile)
cmd.Parameters.AddWithValue("$text_data",cached_textfile.TextData);
cmd.ExecuteNonQuery();
}
public static void ForceInsert(this Database database,CachedTextfile cached_textfile)
{
SqliteCommand cmd = database.Connection.CreateCommand();
cmd.CommandText = $"INSERT INTO {CachedTextfile.TABLE_NAME} (hiberlite_id,shortname,source_file,text_data) VALUES ($hiberlite_id,$shortname,$source_file,$text_data);";
cmd.Parameters.AddWithValue("$hiberlite_id",cached_textfile.HiberliteId);
cmd.Parameters.AddWithValue("$shortname",cached_textfile.Shortname);
cmd.Parameters.AddWithValue("$source_file",cached_textfile.SourceFile);
cmd.Parameters.AddWithValue("$text_data",cached_textfile.TextData);
cmd.ExecuteNonQuery();
}
public static void Insert(this Database database,IEnumerable<CachedTextfile> items)
{
foreach(CachedTextfile cached_textfile in items)
Insert(database,cached_textfile);
}
public static void ForceInsert(this Database database,IEnumerable<CachedTextfile> items)
{
foreach(CachedTextfile cached_textfile in items)
ForceInsert(database,cached_textfile);
}
public static void Upsert(this Database database,CachedTextfile cached_textfile)
{
if(ExistsCachedTextfile(database,cached_textfile))
if(ExistsCachedTextfile(database,cached_textfile.HiberliteId))
{
Update(database,cached_textfile);
return;
Expand All @@ -42,6 +57,20 @@ public static void Upsert(this Database database,IEnumerable<CachedTextfile> ite
foreach(CachedTextfile cached_textfile in items)
Upsert(database,cached_textfile);
}
public static void Delsert(this Database database,CachedTextfile cached_textfile)
{
if(ExistsCachedTextfile(database,cached_textfile.HiberliteId))
{
DeleteCachedTextfile(database,cached_textfile.HiberliteId);
return;
}
ForceInsert(database,cached_textfile);
}
public static void Delsert(this Database database,IEnumerable<CachedTextfile> items)
{
foreach(CachedTextfile cached_textfile in items)
Delsert(database,cached_textfile);
}
public static void Update(this Database database,CachedTextfile cached_textfile)
{
SqliteCommand cmd = database.Connection.CreateCommand();
Expand Down
31 changes: 30 additions & 1 deletion TFHRES/tables/FilemapRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,29 @@ public static void Insert(this Database database,FilemapRecord filemap_record)
cmd.Parameters.AddWithValue("$type",filemap_record.Type);
cmd.ExecuteNonQuery();
}
public static void ForceInsert(this Database database,FilemapRecord filemap_record)
{
SqliteCommand cmd = database.Connection.CreateCommand();
cmd.CommandText = $"INSERT INTO {FilemapRecord.TABLE_NAME} (hiberlite_id,shortname,source_path,type) VALUES ($hiberlite_id,$shortname,$source_path,$type);";
cmd.Parameters.AddWithValue("$hiberlite_id",filemap_record.HiberliteId);
cmd.Parameters.AddWithValue("$shortname",filemap_record.Shortname);
cmd.Parameters.AddWithValue("$source_path",filemap_record.SourcePath);
cmd.Parameters.AddWithValue("$type",filemap_record.Type);
cmd.ExecuteNonQuery();
}
public static void Insert(this Database database,IEnumerable<FilemapRecord> items)
{
foreach(FilemapRecord filemap_record in items)
Insert(database,filemap_record);
}
public static void ForceInsert(this Database database,IEnumerable<FilemapRecord> items)
{
foreach(FilemapRecord filemap_record in items)
ForceInsert(database,filemap_record);
}
public static void Upsert(this Database database,FilemapRecord filemap_record)
{
if(ExistsFilemapRecord(database,filemap_record))
if(ExistsFilemapRecord(database,filemap_record.HiberliteId))
{
Update(database,filemap_record);
return;
Expand All @@ -42,6 +57,20 @@ public static void Upsert(this Database database,IEnumerable<FilemapRecord> item
foreach(FilemapRecord filemap_record in items)
Upsert(database,filemap_record);
}
public static void Delsert(this Database database,FilemapRecord filemap_record)
{
if(ExistsFilemapRecord(database,filemap_record.HiberliteId))
{
DeleteFilemapRecord(database,filemap_record.HiberliteId);
return;
}
ForceInsert(database,filemap_record);
}
public static void Delsert(this Database database,IEnumerable<FilemapRecord> items)
{
foreach(FilemapRecord filemap_record in items)
Delsert(database,filemap_record);
}
public static void Update(this Database database,FilemapRecord filemap_record)
{
SqliteCommand cmd = database.Connection.CreateCommand();
Expand Down
Loading

0 comments on commit 2d20d2a

Please sign in to comment.