<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK v2: ObjDump / ObjLoad]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=18380</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=18380&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK v2: ObjDump / ObjLoad».]]></description>
		<lastBuildDate>Mon, 24 Feb 2025 12:50:36 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[AHK v2: ObjDump / ObjLoad]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=162000#p162000</link>
			<description><![CDATA[<p>Функция сохранения объекта в файл или память в бинарном (RAW) виде, для последующего восстановления (загрузки) в переменную.</p><div class="codebox"><pre><code>
/* example; comment this line to try
o:={}
o.var:=1234567890
o.val:=&quot;test object property&quot;
o.arr:=[{},{}]
o.arr[1].par:=&quot;asdf&quot;
o.arr[1].val:=1234
o.arr[2].par:=&quot;jkl;&quot;
o.arr[2].val:=5678
o.m:=map()
o.m.var:=123.0
o.m.val:=&quot;qwer&quot;
o.m[11]:={}
o.m[11].par:=&quot;asdf&quot;
o.m[11].val:=1234
o.m[22]:={}
o.m[22].par:=&quot;jkl;&quot;
o.m[22].vasl:=5678
o.b:=Buffer(3,255)

SaveFileName:=a_desktop &quot;\Dumped Object.test&quot;
ObjDump(SaveFileName,o)
MyObj:=ObjLoad(SaveFileName)

msgbox MyObj.var
msgbox MyObj.val
msgbox MyObj.arr[1].par
msgbox MyObj.arr[1].val
msgbox MyObj.arr[2].par
msgbox MyObj.arr[2].val
msgbox MyObj.m.var
msgbox MyObj.m.val
msgbox MyObj.m[11].par
msgbox MyObj.m[11].val
msgbox MyObj.m[22].par
msgbox MyObj.m[22].vasl
msgbox NumGet(o.b,2,&quot;UChar&quot;)
exitapp
esc::exitapp
/*
*/
ObjLoad(addr,&amp;offset:=0) ; load dumped object to variable; from memory (addr is int) or from file (addr is string)
{
  ; #requires AutoHotkey v2.0.19
  If !IsNumber(addr)
  {
    If !FileExist(addr)
      return
    sz:=FileGetSize(addr)
    If !sz
      return
    Data:=FileRead(addr,&quot;RAW&quot;)
    addr:=Data.Ptr
    sz:=NumGet(addr,&quot;Int64&quot;)
    addr+=8
    obj:=0
  }
  ;    Sizes          1       1       2        2     4      4       8        8        8    x     x     x      x         x
  ;    Type.Enum: Char.1 UChar.2 Short.3 UShort.4 Int.5 UInt.6 Int64.7 UInt64.8 Double.9 Str.10 [].11 {}.12 map().13 buffer.14
  static types:=[&quot;Char&quot;,&quot;UChar&quot;,&quot;Short&quot;,&quot;UShort&quot;,&quot;Int&quot;,&quot;UInt&quot;,&quot;Int64&quot;,&quot;UInt64&quot;,&quot;Double&quot;]
  static syzes:=[     2,      2,      3,       3,    5,     5,      9,       9,       9]
  typ:=NumGet(addr,offset,&quot;Char&quot;)
  if (typ&lt;10)
  {
    obj:=NumGet(addr,offset+1,types[typ])
    offset+=syzes[typ]
  }
  else if (typ=10)
  {
    size:=NumGet(addr,offset+1,&quot;Int64&quot;)
    obj:=StrGet(addr+offset+9,size)
    offset+=9+size
  }
  else if (typ=11)
  {
    obj:=[]
    size:=NumGet(addr,offset+1,&quot;Int64&quot;)
    obj.Length:=size
    offset+=9
    loop size
      obj[a_index]:=ObjLoad(addr,&amp;offset)
  }
  else if (typ=12)
  {
    obj:={}
    Props:=NumGet(addr,offset+1,&quot;Int64&quot;)
    offset+=9
    loop Props
    {
      k:=ObjLoad(addr,&amp;offset)
      obj.%k%:=ObjLoad(addr,&amp;offset)
    }
  }
  else if (typ=13)
  {
    obj:=Map()
    Props:=NumGet(addr,offset+1,&quot;Int64&quot;)
    Items:=NumGet(addr,offset+9,&quot;Int64&quot;)
    offset+=17
    loop Props
    {
      k:=ObjLoad(addr,&amp;offset)
      obj.%k%:=ObjLoad(addr,&amp;offset)
    }
    loop Items
    {
      i:=ObjLoad(addr,&amp;offset)
      obj[i]:=ObjLoad(addr,&amp;offset)
    }
  }
  else if (typ=14)
  {
    size:=NumGet(addr,offset+1,&quot;Int64&quot;)
    obj:=Buffer(size)
    offset+=9
    loop size
      NumPut(&quot;UChar&quot;,NumGet(addr,offset+a_index-1,&quot;UChar&quot;),obj,a_index-1)
    offset+=size
  }
  return obj
}

ObjDump(Path,obj) ; dump object to file
{
  ; #requires AutoHotkey v2.0.19
  if !sz:=RawObjectSize(obj)
    return
  If FileExist(Path)
    FileDelete Path
  sz+=8
  Data:=Buffer(sz,0)
  ptr:=Data.Ptr
  NumPut(&quot;Int64&quot;,sz-8,ptr) ;size of all data
  RawObject(obj,ptr+8) ; use this for dumping to memory instead of ObjDump
  f:=FileOpen(Path,&quot;rw-rwd&quot;,&quot;CP0&quot;)
  Loop (sz//65536)
    f.RawWrite(ptr,65536),ptr+=65536
  f.RawWrite(ptr,Mod(sz,65536))
  f.Close()
  return sz
}
RawObject(obj,ptr,offset:=0) ; dump object to memory
{
  ; #requires AutoHotkey v2.0.19
  ;    Sizes          1       1       2        2     4      4       8        8        8    x     x     x      x         x
  ;    Type.Enum: Char.1 UChar.2 Short.3 UShort.4 Int.5 UInt.6 Int64.7 UInt64.8 Double.9 Str.10 [].11 {}.12 map().13 buffer.14
  if isObject(Obj)
  {
    if (Obj.base=[].base)
    {
      NumPut(&quot;Char&quot;,11,ptr,offset) ; type
      NumPut(&quot;Int64&quot;,Obj.Length,ptr,offset+1) ; Length of array
      offset+=9
      loop Obj.Length
        offset:=RawObject(Obj[a_index],ptr,offset)
    }
    else if (Obj.base={}.base)
    {
      NumPut(&quot;Char&quot;,12,ptr,offset) ; type
      NumPut(&quot;Int64&quot;,ObjOwnPropCount(Obj),ptr,offset+1) ; num of Props
      offset+=9
      for k,v in Obj.OwnProps()
      {
        offset:=RawObject(k,ptr,offset) ; name of prop (str)
        offset:=RawObject(v,ptr,offset) ; value of prop (any)
      }
    }
    else if (Obj.base=Map().base)
    {
      NumPut(&quot;Char&quot;,13,ptr,offset) ; type
      NumPut(&quot;Int64&quot;,ObjOwnPropCount(Obj),ptr,offset+1) ; num of Props
      NumPut(&quot;Int64&quot;,Obj.Count,ptr,offset+9) ; num of items
      offset+=17
      for k,v in Obj.OwnProps()
      {
        offset:=RawObject(k,ptr,offset) ; name of prop (str)
        offset:=RawObject(v,ptr,offset) ; value of prop (any)
      }
      for k,v in Obj
      {
        offset:=RawObject(k,ptr,offset) ; index of item (any)
        offset:=RawObject(v,ptr,offset) ; value of item (any)
      }
    }
    else if (Obj.base=Buffer().base)
    {
      NumPut(&quot;Char&quot;,14,ptr,offset) ; type
      NumPut(&quot;Int64&quot;,Obj.size,ptr,offset+1) ; num of bytes
      offset+=9
      loop Obj.size
        NumPut(&quot;UChar&quot;,NumGet(Obj,a_index-1,&quot;UChar&quot;),ptr,offset+a_index-1) ; Buffer content (RAW)
      offset+=Obj.size
    }
  }
  else if IsNumber(obj) ; num
  {
    NumPut(&quot;Char&quot;,IsFloat(Obj)?9:Obj&gt;4294967295?8       :Obj&gt;65535?6     :Obj&gt;255?4       :Obj&gt;-1?2      :Obj&gt;-129?1     :Obj&gt;-32769?3      :Obj&gt;-2147483649?5    :7,ptr,offset+0)
    NumPut(IsFloat(Obj)?&quot;Double&quot;:Obj&gt;4294967295?&quot;UInt64&quot;:Obj&gt;65535?&quot;UInt&quot;:Obj&gt;255?&quot;UShort&quot;:Obj&gt;-1?&quot;UChar&quot;:Obj&gt;-129?&quot;Char&quot;:Obj&gt;-32769?&quot;Short&quot;:Obj&gt;-2147483649?&quot;Int&quot;:&quot;Int64&quot;,Obj,ptr,offset+1)
    offset+=       IsFloat(Obj)||Obj&gt;4294967295?9       :Obj&gt;65535?5     :Obj&gt;255?3                      :Obj&gt;-129?2     :Obj&gt;-32769?3      :Obj&gt;-2147483649?5    :9
  }
  else ; string
  {
    NumPut(&quot;Char&quot;,10,ptr,offset)  ; type
    NumPut(&quot;Int64&quot;,sz:=StrPut(Obj,ptr+offset+9),ptr,offset+1) ; (str size) string text
    offset+=sz+9
  }
  return offset
}

RawObjectSize(Obj,sz:=0) ; get object&#039;s size in dumped condition
{
  ; #requires AutoHotkey v2.0.19
  if isObject(Obj)
  {
    if (Obj.base=[].base)
    {
      sz+=9 ; 1 (type) + 8 (num of items)
      loop Obj.Length
        sz:=RawObjectSize(Obj[a_index],sz)
    }
    else if (Obj.base={}.base)
    {
      sz+=9 ; 1 (type) + 8 (num of prop)
      for k,v in Obj.OwnProps()
      {
        sz:=RawObjectSize(k,sz) ; name of prop (str)
        sz:=RawObjectSize(v,sz) ; value of prop (any)
      }
    }
    else if (Obj.base=Map().base)
    {
      sz+=17 ; 1 (type) + 8 (size of Props) + 8 (size of array)
      for k,v in Obj.OwnProps()
      {
        sz:=RawObjectSize(k,sz) ; name of prop (str)
        sz:=RawObjectSize(v,sz) ; value of prop (any)
      }
      for k,v in Obj
      {
        sz:=RawObjectSize(k,sz) ; index
        sz:=RawObjectSize(v,sz) ; value
      }
    }
    else if (Obj.base=Buffer().base) ; sz = 1 (type) + 8 (size) + (buffer size)
      sz+=9+Obj.size
    else
    {
      MsgBox &quot;Unsupported object type: `&quot;&quot; Type(obj) &quot;`&quot;.&quot;,&quot;ObjDump&quot;,0x30
      return 0
    }
  }
  else if IsNumber(obj) ; sz = 1 (type) + (size of data type)
    sz+=IsFloat(Obj)||Obj&gt;4294967295?9:Obj&gt;65535?5:Obj&gt;255?3:Obj&gt;-129?2:Obj&gt;-32769?3:Obj&gt;-2147483649?5:9
  else ; sz = 1 (type) + 8 (size) + (str size)
    sz+=9+StrPut(Obj)
  return sz
}


</code></pre></div><p><a href="https://forum.script-coding.com/viewtopic.php?pid=162001#p162001">Тема для обсуждения.</a></p><p><a href="https://www.autohotkey.com/boards/viewtopic.php?t=3573">Версия для AHK v1.</a></p>]]></description>
			<author><![CDATA[null@example.com (Alectric)]]></author>
			<pubDate>Mon, 24 Feb 2025 12:50:36 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=162000#p162000</guid>
		</item>
	</channel>
</rss>
