Skip to content

Instantly share code, notes, and snippets.

@draqoon
Last active May 8, 2019 01:51
Show Gist options
  • Save draqoon/30f17be3cdf7015f424c2e985ae2d2f3 to your computer and use it in GitHub Desktop.
Save draqoon/30f17be3cdf7015f424c2e985ae2d2f3 to your computer and use it in GitHub Desktop.
[Terraria] テラリアのMODファイルを展開するサンプル。
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
class Program {
static void Main( string[] args ) {
var modPath = @"%USERPROFILE%\Documents\My Games\Terraria\ModLoader\Mods\CalamityMod.tmod";
var outputDir = @"C:\Temp";
modPath = Environment.ExpandEnvironmentVariables( modPath );
if( !File.Exists( modPath ) ) {
throw new FileNotFoundException( "warning!! mod not found", modPath );
}
using( var fileStream = File.OpenRead( modPath ) ) {
using( var binaryReader = new BinaryReader( fileStream ) ) {
if( Encoding.ASCII.GetString( binaryReader.ReadBytes( 4 ) ) != "TMOD" ) {
throw new Exception( "Magic Header != \"TMOD\"" );
}
var tModLoaderVersion = new Version( binaryReader.ReadString() );
Console.WriteLine( $"ModLoader Version: {tModLoaderVersion}" );
var hash = binaryReader.ReadBytes( 20 );
var signature = binaryReader.ReadBytes( 256 );
var num = binaryReader.ReadInt32();
using( var deflateStream = new DeflateStream( fileStream, (CompressionMode)0 ) ) {
using( var binaryReader2 = new BinaryReader( deflateStream ) ) {
var name = binaryReader2.ReadString();
var version = new Version( binaryReader2.ReadString() );
var num2 = binaryReader2.ReadInt32();
Console.WriteLine( $"Mod Version: {version}\n" );
if( !Directory.Exists( outputDir ) ) {
Directory.CreateDirectory( outputDir );
}
for( var i = 0; i < num2; i++ ) {
var text = binaryReader2.ReadString();
var size = binaryReader2.ReadInt32();
var data = binaryReader2.ReadBytes( size );
Console.Write( $"{i:0000}: {text} ..." );
var subPath = text.Replace( '/', Path.DirectorySeparatorChar );
var filePath = Path.Combine( outputDir, subPath );
var fileDir = Path.GetDirectoryName( filePath );
if( !Directory.Exists( fileDir ) ) {
Directory.CreateDirectory( fileDir );
}
using( var outStream = new FileStream( filePath, FileMode.OpenOrCreate ) ) {
using( var writer = new BinaryWriter( outStream ) ) {
writer.Write( data );
}
}
Console.WriteLine( " Done." );
}
Console.WriteLine( "\nFinish." );
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment