File size: 1,360 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
--[[
	Project: SA Memory (Available from https://blast.hk/)
	Developers: LUCHARE, FYP

	Special thanks:
		plugin-sdk (https://github.com/DK22Pac/plugin-sdk) for the structures and addresses.

	Copyright (c) 2018 BlastHack.
]]

local mt = require 'SAMemory.metatype'
local ffi = require 'ffi'

local cdef = ffi.cdef

local module = {
	ffi = ffi;
}

function module.ffi.cdef(def)
	local defn, c = def:gsub('struct%s*([_%a%d]+)%s*:%s*(%a*%s*[_%a%d]+)[\n%s]*{', 'struct %1 { %2 __parent;')

	cdef(defn)

	if c == 0 then return end

	for child in def:gmatch('(struct%s*[%a%d%p]+)%s*:') do
		mt.set_handler(
			child,
			'__index',
			function(s, k)
				return s.__parent[k] or error(('%s has no member named "%s"'):format(child, k or '?'))
			end
		)
		mt.set_handler(
			child,
			'__newindex',
			function(s, k, v)
				s.__parent[k] = v
			end
		)
	end
end

function module.require(mod)
	if not package.loading then package.loading = {} end
	mod = 'SAMemory.game.' .. mod
	if package.loading[mod] == nil then
		package.loading[mod] = true
		local v = {require(mod)}
		package.loading[mod] = nil
		return unpack(v)
	end
end

function module.validate_size(struct, size)
	local sizeof = ffi.sizeof(struct) or 0
	assert(sizeof == size, ("validate_size('%s', %d) assertion failed! Expected size 0x%X, got 0x%X."):format(struct, size, size, sizeof))
end

return module