Skip to content

Commit

Permalink
[hlmem] refacto for hide integration (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiaomao authored Jan 7, 2025
1 parent 9b768f2 commit 7fcec88
Show file tree
Hide file tree
Showing 8 changed files with 884 additions and 614 deletions.
108 changes: 108 additions & 0 deletions other/haxelib/hlmem/Analyzer.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package hlmem;

import hlmem.Memory;
using format.hl.Tools;

// A list of ansi colors is available at
// https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797#8-16-colors
enum abstract TextColor(Int) {
var Black = 30;
var Red = 31;
var Green = 32;
var Yellow = 33;
var Blue = 34;
var Magenta = 35;
var Cyan = 36;
var White = 37;
}

class Analyzer {

public static var displayProgress = true;
public static var displayFields : FieldsMode = Full;
public static var maxLines : Int = 100;
public static var useColor : Bool = true;

public var code : format.hl.Data;
var mem : Memory;
var otherMems : Array<Memory> = [];

public function new() {
}

public function loadBytecode( file : String ) {
if( code != null ) throw "Duplicate code";
code = new format.hl.Reader(false).read(new haxe.io.BytesInput(sys.io.File.getBytes(file)));
Analyzer.log(file + " code loaded");
}

public function loadMemoryDump( file : String ) : Memory {
var m = new Memory(this);
m.load(file);
if( mem == null ) {
mem = m;
} else {
otherMems.push(m);
}
return m;
}

public function build( filter : FilterMode = None ) {
mem.build();
for( m2 in otherMems ) {
m2.buildBlockTypes();
}
mem.otherMems = [for (i in otherMems) i];
mem.filterMode = filter;
mem.buildFilteredBlocks();
}

public function nextDump() : Memory {
otherMems.push(mem);
mem = otherMems.shift();
mem.otherMems = [for (i in otherMems) i];
mem.build();
mem.buildFilteredBlocks();
var ostr = otherMems.length > 0 ? (" (others are " + otherMems.map(m -> m.memFile) + ")") : "";
Analyzer.log("Using dump " + mem.memFile + ostr);
return mem;
}

public inline function getMainMemory() {
return mem;
}

public function getMemStats() : Array<hlmem.Result.MemStats> {
var objs = [mem.getMemStats()];
for( m2 in otherMems ) {
objs.push(m2.getMemStats());
}
return objs;
}

public static function mb( v : Float ) : String {
if( v < 1000 )
return Std.int(v) + "B";
if( v < 1024 * 1000 )
return (Math.round(v * 10 / 1024) / 10)+"KB";
return (Math.round(v * 10 / (1024 * 1024)) / 10)+"MB";
}

public static inline function logProgress( current : Int, max : Int ) {
if( displayProgress && current % 1000 == 0 )
Sys.print(Std.int((current * 1000.0 / max) / 10) + "% \r");
if( displayProgress && current == max )
Sys.print(" \r");
}

public static inline function log( msg : String ) {
Sys.println(msg);
}

public static inline function withColor( str : String, textColor : TextColor ) {
if( !useColor )
return str;
return '\x1B[${textColor}m${str}\x1B[0m';
}

}
16 changes: 13 additions & 3 deletions other/haxelib/hlmem/Block.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ abstract Pointer(haxe.Int64) {
return haxe.Int64.shr(this,k);
}

@:op(A == B)
public static function eq( a : Pointer, b : Pointer ) : Bool {
return a.value == b.value;
}

@:op(A != B)
public static function neq( a : Pointer, b : Pointer ) : Bool {
return a.value != b.value;
}

public static var NULL(get,never) : Pointer;
inline static function get_NULL() return new Pointer(0);

Expand All @@ -41,7 +51,7 @@ class Page {
public var kind : PageKind;
public var size : Int;
public var reserved : Int;
public var dataPosition : Int = -1;
public var dataPosition : Float;

public function new() {
}
Expand Down Expand Up @@ -108,7 +118,7 @@ class Block {
parents.push(b);
}
if( b.subs == null ) b.subs = [];
b.subs.push(new BlockSub(this,fid));
b.subs.push(new BlockSub(this, fid));
}

public function makeTID( prev : Block, withField : Bool ) {
Expand Down Expand Up @@ -161,7 +171,7 @@ class Block {
owner = parents[0];
}

function removeParent( p : Block ) {
public function removeParent( p : Block ) {
if( parents != null ) {
parents.remove(p);
if( parents.length == 0 ) parents = null;
Expand Down
111 changes: 111 additions & 0 deletions other/haxelib/hlmem/FileReader.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package hlmem;

#if js
enum FileSeekMode {
SeekBegin;
SeekCur;
SeekEnd;
}
#else
typedef FileSeekMode = sys.io.FileSeek;
#end

class FileReader {

#if js
// js read file in once for better performance
var memBytes : haxe.io.Bytes;
var memPos : Int;
#else
var memInput : sys.io.FileInput;
#end

public inline function new(file : String) {
#if js
memBytes = sys.io.File.getBytes(file);
memPos = 0;
#else
memInput = sys.io.File.read(file);
#end
}

public inline function close() {
#if js
memBytes = null;
memPos = 0;
#else
if( memInput != null )
memInput.close();
memInput = null;
#end
}

public inline function readString(length : Int) : String {
#if js
var str = memBytes.getString(memPos, 3);
memPos += 3;
#else
var str = memInput.read(3).toString();
#end
return str;
}

public inline function readByte() : Int {
#if js
var b = memBytes.get(memPos);
memPos += 1;
#else
var b = memInput.readByte();
#end
return b;
}

public inline function readInt32() : Int {
#if js
var i = memBytes.getInt32(memPos);
memPos += 4;
#else
var i = memInput.readInt32();
#end
return i;
}

public inline function readPointer( is64 : Bool ) : Block.Pointer {
var low = readInt32();
var high = is64 ? readInt32() : 0;
return cast haxe.Int64.make(high,low);
}

public inline function tell() : Float {
#if js
return memPos;
#else
return tell2(@:privateAccess memInput.__f);
#end
}

#if (hl && hl_ver >= version("1.12.0"))
@:hlNative("std","file_seek2") static function seek2( f : sys.io.File.FileHandle, pos : Float, mode : Int ) : Bool { return false; }
@:hlNative("std","file_tell2") static function tell2( f : sys.io.File.FileHandle ) : Float { return 0; }
#end

// pos will be cast to Int64
public inline function seek( pos : Float, mode : FileSeekMode ) {
#if js
if( pos > 0x7FFFFFFF ) throw haxe.io.Error.Custom("seek out of bounds");
var dpos = Std.int(pos);
switch( mode ) {
case SeekBegin:
memPos = dpos;
case SeekCur:
memPos += dpos;
case SeekEnd:
memPos = memBytes.length + dpos;
}
#else
if( !seek2(@:privateAccess memInput.__f, pos, mode.getIndex()) )
throw haxe.io.Error.Custom("seek2 failure()");
#end
}

}
Loading

0 comments on commit 7fcec88

Please sign in to comment.