forked from bmx-ng/bmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmk_zap.bmx
138 lines (105 loc) · 2.81 KB
/
bmk_zap.bmx
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Strict
Import "bmk_modutil.bmx"
Import "bmk_bank.bmx"
Import "bmk_modinfo.bmx"
Function Zap( path$,stream:TStream )
If Not path Return False
Local name$=StripDir( path )
Local skip=False
If name[..1]="."
skip=True
Else If name.ToLower().EndsWith( ".bak" )
skip=True
EndIf
If skip
stream.WriteLine ""
Return True
EndIf
Local Mode=FileMode( path )
Select FileType(path)
Case FILETYPE_NONE
Print "Error zapping file "+path
Return
Case FILETYPE_FILE
Local size=FileSize(path)
stream.WriteLine name
stream.WriteLine Mode
stream.WriteLine size
Local from_stream:TStream=ReadStream(path)
CopyBytes from_stream,stream,size
from_stream.Close
Case FILETYPE_DIR
Local dir$[]=LoadDir( path )
Local size=Len( dir )
stream.WriteLine name
stream.WriteLine -Mode
stream.WriteLine size
For Local t$=EachIn dir
If Not Zap( path+"/"+t,stream ) Return
Next
End Select
Return True
End Function
Function Unzap( dir$,stream:TStream )
Local name$=stream.ReadLine()
If Not name Return True
Local Mode=Int( stream.ReadLine() )
Local size=Int( stream.ReadLine() )
Local path$=dir+"/"+name
If Mode<0
Mode=-Mode
CreateDir path
For Local k=0 Until size
If Not Unzap( path,stream ) Return
Next
Else
DeleteFile path
Local to_stream:TStream=WriteStream(path)
CopyBytes stream,to_stream,size
to_stream.Close
EndIf
SetFileMode path,Mode
Return True
End Function
Function ZapMod( name$,stream:TStream )
Local path$=ModuleInterface( name,"release."+opt_target_platform+"."+opt_arch )
If FileType(path)<>FILETYPE_FILE
Print "Failed to find module"
Return
EndIf
Local src:TSourceFile=ParseSourceFile( path )
stream.WriteLine "Module: "+name
For Local t$=EachIn src.info
stream.WriteLine t
Next
stream.WriteLine ""
Local bank:TBank=TBank.Create(0)
Local bank_stream:TStream=TBankStream.Create( bank )
If Not Zap( ModulePath(name),bank_stream ) Throw "Failed to publish module"
bank_stream.Close
bank=CompressBank( bank )
bank_stream=TBankStream.Create( bank )
CopyStream bank_stream,stream
bank_stream.Close
End Function
Function UnzapMod( stream:TStream )
Local modinfo:TModInfo=TModInfo.CreateFromStream( stream )
Local path$=ModulePath( modinfo.name )
If Not CreateDir( path,True ) Throw "Unable to create module directory"
DeleteDir path,True
Local bank:TBank=TBank.Create(0)
Local bank_stream:TStream=TBankStream.Create( bank )
CopyStream stream,bank_stream
bank_stream.Close
bank=UncompressBank( bank )
bank_stream=TBankStream.Create( bank )
If Not Unzap( ExtractDir(path),bank_stream )
Print "Failed to Unzap module"
Return
EndIf
bank_stream.Close
?MacOS
Ranlib path
?
Return True
End Function