-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hlmem] refacto for hide integration (#739)
- Loading branch information
Showing
8 changed files
with
884 additions
and
614 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
Oops, something went wrong.