using System;
using System.IO;
using System.Text;
namespace fx.meta.bgi.util
{
public sealed class ExtractText
{
public static void Main( string[ ] args ) {
if ( args.Length < 1 ) {
Console.WriteLine( "Give a valid script file as the first parameter." );
return;
}
string infilename = args[ 0 ];
string outfilename = args[ 0 ] + ".txt";
FileInfo infile = new FileInfo( infilename );
if ( !infile.Exists ) {
Console.WriteLine( "Give a valid script file as the first parameter." );
return;
}
long filelen = infile.Length;
// read all the text and write to output
Encoding utf16le = new UnicodeEncoding( false, true );
Encoding jis = Encoding.GetEncoding( 932 );
using ( BinaryReader reader = new BinaryReader( infile.OpenRead( ), jis ) ) {
using ( BinaryWriter writer = new BinaryWriter( File.Create( outfilename ), utf16le ) ) {
reader.BaseStream.Seek( 0x020, SeekOrigin.Begin );
uint opcode = reader.ReadUInt32( );
uint codeSize = reader.ReadUInt32( );
if ( opcode != 0x07F ) {
throw new Exception( "Unsupported script. Expecting 0x7F at 0x20, but found " + opcode.ToString( "X" ) );
}
if ( codeSize > filelen ) {
throw new Exception( "Bad script. Code size greater than file size." );
}
writer.Write( ( ushort ) 0xFEFF );
reader.BaseStream.Seek( ( long ) codeSize, SeekOrigin.Begin );
StringBuilder builder = null;
while ( reader.BaseStream.Position < reader.BaseStream.Length ) {
string position = reader.BaseStream.Position.ToString( "X" );
builder = new StringBuilder( );
char c = '\0';
while ( ( c = reader.ReadChar( ) ) != '\0' ) {
if ( c == '\n' ) {
builder.Append( @"\n" );
} else {
builder.Append( c );
}
}
string text = builder.ToString( );
string strlen = jis.GetByteCount( text ).ToString( );
writer.Write( utf16le.GetBytes( string.Format( "{0}, {1}, {2}{3}", position, strlen, text, Environment.NewLine ) ) );
}
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace fx.meta.bgi.util
{
public sealed class InsertText
{
public static void Main( string[ ] args ) {
if ( args.Length < 1 ) {
Console.WriteLine( "Give a valid script file as the first parameter." );
return;
}
string scriptName = args[ 0 ];
string textName = scriptName + ".txt";
string newScriptName = scriptName + ".new";
FileInfo scriptFile = new FileInfo( scriptName );
if ( !scriptFile.Exists ) {
Console.WriteLine( "Give a valid script file as the first parameter." );
return;
}
if ( !File.Exists( textName ) ) {
Console.WriteLine( "Correspoding text file not available." );
return;
}
long filelen = scriptFile.Length;
// read all the text and write to output
Encoding utf16le = new UnicodeEncoding( false, true );
Encoding jis = Encoding.GetEncoding( 932 );
Encoding gbk = Encoding.GetEncoding( 936 );
using ( BinaryReader script = new BinaryReader( scriptFile.OpenRead( ), jis ) ) {
using ( StreamReader text = new StreamReader( File.OpenRead( textName ), utf16le ) ) {
using ( BinaryWriter writer = new BinaryWriter( File.Create( newScriptName ), gbk ) ) {
Dictionary<uint, uint> offsetMapping = new Dictionary<uint, uint>( );
List<string> newTexts = new List<string>( );
int offsetDifference = 0;
script.BaseStream.Seek( 0x020, SeekOrigin.Begin );
uint opcode = script.ReadUInt32( );
uint codeSize = script.ReadUInt32( );
if ( opcode != 0x07F ) {
throw new Exception( "Unsupported script. Expecting 0x7F at 0x20, but found " + opcode.ToString( "X" ) );
}
if ( codeSize > filelen ) {
throw new Exception( "Bad script. Code size greater than file size." );
}
uint currentOffset = codeSize;
string line = text.ReadLine( );
while ( line != null && !line.Equals( string.Empty ) ) {
string[ ] elem = line.Split( new string[ ] { ", " }, StringSplitOptions.RemoveEmptyEntries );
uint oldOffset = UInt32.Parse( elem[ 0 ], NumberStyles.AllowHexSpecifier );
offsetMapping.Add( oldOffset, ( uint ) ( oldOffset + offsetDifference ) );
Console.WriteLine( "{0}, {1}, {2}",
oldOffset.ToString( "X" ),
( offsetMapping[ oldOffset ] ).ToString( "X" ),
offsetDifference.ToString( ) );
int oldTextLength = Int32.Parse( elem[ 1 ] );
int newTextLength = 0;
if ( 2 < elem.Length ) {
newTextLength = gbk.GetByteCount( elem[ 2 ] );
string s = elem[ 2 ].Replace( @"\n", "\n" );
newTexts.Add( s );
} else {
newTextLength = 0;
newTexts.Add( string.Empty );
}
offsetDifference += ( int ) ( newTextLength - oldTextLength );
line = text.ReadLine( );
}
script.BaseStream.Seek( 0, SeekOrigin.Begin );
byte[ ] scriptBuffer = script.ReadBytes( ( int ) codeSize );
for ( int dwptr = 0; dwptr < codeSize; dwptr += 4 ) {
uint currentDw = scriptBuffer[ dwptr ];
if ( currentDw != 0x03 && currentDw != 0x07F )
continue;
dwptr += 4;
currentDw = ToUInt32( scriptBuffer, dwptr );
if ( ( currentDw >= codeSize )
&& ( offsetMapping.ContainsKey( currentDw ) ) ) {
uint newOfs = offsetMapping[ currentDw ];
WriteUInt32( newOfs, scriptBuffer, dwptr );
} else {
dwptr -= 4;
}
}
writer.Write( scriptBuffer );
foreach ( string s in newTexts ) {
writer.Write( gbk.GetBytes( s ) );
writer.Write( ( byte ) 0 );
}
}
}
}
}
static uint ToUInt32( byte[ ] array, int beginOfs ) {
return ( uint ) (
( array[ beginOfs + 3 ] << 24 ) |
( array[ beginOfs + 2 ] << 16 ) |
( array[ beginOfs + 1 ] << 8 ) |
( array[ beginOfs ] ) );
}
static void WriteUInt32( uint data, byte[ ] array, int beginOfs ) {
array[ beginOfs ] = ( byte ) ( ( data ) & 0x0FF );
array[ beginOfs + 1 ] = ( byte ) ( ( data >> 8 ) & 0x0FF );
array[ beginOfs + 2 ] = ( byte ) ( ( data >> 16 ) & 0x0FF );
array[ beginOfs + 3 ] = ( byte ) ( ( data >> 24 ) & 0x0FF );
}
}
}
分享到:
相关推荐
fortune 是一个超媒体 API 原型框架,实现 JSON API 规范。fortune 具有一个模块化的持久层,里面包括了 NeDB (内联), MongoDB, MySQL, Postgres 和 SQLite 的适配器,可以查看 引导手册 了解如何使用。Fortune 实现...
matlab开发-Fortune。在命令行中打印随机的,有时是有趣的财富
wheel-of-fortune-in-unity
Fortune Feb ChatGPT Cryptod.pdf Fortune Feb ChatGPT Cryptod.pdf Fortune Feb ChatGPT Cryptod.pdf Fortune Feb ChatGPT Cryptod.pdf Fortune Feb ChatGPT Cryptod.pdf Fortune Feb ChatGPT Cryptod.pdf
fortune-teller, 还有另一个很棒的spring 云 FortuneFortune 是由两个服务组成的一个非常基本的应用程序:财富服务 - 提供随机的中国幸运饼干财富财富 UI 提供了一个使用财富服务的UI它利用来自 spring 云和 Netflix...
这是Fortune.js的IndexedDB适配器。 包括各种性能和兼容性优化: 在Web Worker中运行,这样数据库操作不会,并使用进行。 延迟加载记录,然后将其保存在内存中。 主键在全球范围内是唯一的,从而解决了一些。 $ ...
Unity下的转盘插件,用于游戏里的各种转盘功能,例如抽奖,选择等功能。
fortune-mod维护版本和持续开发 该GitHub存储库保留了fortune-mod( 一个版本)的源代码。 fortune是一个命令行实用程序,它显示引号集合中的随机报价。 该集合是从本地读取的,不需要网络访问。 下载中提供了大量...
cowsay+fortune rpm安装包,其中还包括依赖包的rpm安装包,适合离线安装,同事提供cowsay+fortune组合使用的脚本 测试系统版本:Cent OS 7.3
Fortune Wheel
世界500强公司(下)
voronoi图的Fortune算法源码,visual c++(Fortune s algorithm source code of voronoi diagram with visual c++)
Fortune.js 介绍 该存储库包含一个很小的财富报价数据库(172)和一个读取报价并返回随机报价的库。 如何使用 < script type =" text/javascript " src =" fortune.js " > </ script > fortune . ...
通过从 npm 安装来获取它: $ npm install fortune1.0之路JSON API 的v1.0有一个发布标签,但 Fortune 尚未实现整个功能集。 需要做什么: 查询、分页、排序复合文件不同格式之间的内容协商(未来,计划) 确保规范...
财富API 从unix fortune命令获取随机或特定的运气
Fortune Feb ChatGPT Cryptod.pdf.zip