-
Notifications
You must be signed in to change notification settings - Fork 789
/
CMakeLists.txt
418 lines (343 loc) · 13 KB
/
CMakeLists.txt
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMakeModules")
if (WIN32)
cmake_minimum_required(VERSION 3.11)
project("Taiwu_Mods" CSharp)
set(CMAKE_CSharp_FLAGS "/langversion:latest")
include(CSharpUtilities)
SET(DLLPATH "The Scroll Of Taiwu Alpha V1.0_Data/Managed/")
endif()
if(UNIX)
cmake_minimum_required(VERSION 2.6.2)
project(Taiwu_Mods NONE)
# Define generator properties
set(VS10SLN OFF CACHE BOOL "Force generation of SLN.")
# Find C#
find_package( CSharp REQUIRED )
include( ${CSHARP_USE_FILE} )
# Find MSBuild
find_package( MSBuild REQUIRED )
include( ${MSBuild_USE_FILE} )
# Setup build locations.
if(NOT CMAKE_RUNTIME_OUTPUT_DIR)
set(CMAKE_RUNTIME_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
endif()
if(NOT CMAKE_LIBRARY_OUTPUT_DIR)
set(CMAKE_LIBRARY_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
endif()
# Make sure the nested directory structure exists
file(MAKE_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIR})
file(MAKE_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIR})
# If no build type is set it should defaults to debug
if( NOT CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose a build type: [Debug Release]" FORCE )
endif()
# Set some default build flags.
set(CSBUILDFLAGS "")
set(MSBUILDFLAGS "")
# Set up the compiler flag for Debug/Release mode.
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND CSBUILDFLAGS "/debug:full" "/debug+" "/optimize-")
list(APPEND MSBUILDFLAGS "/p:Configuration=Debug")
else()
list(APPEND CSBUILDFLAGS "/debug-" "/optimize+")
list(APPEND MSBUILDFLAGS "/p:Configuration=Release")
endif()
# Define whether to use assembly deleyed signing or not
set(DELAY_SIGN OFF CACHE BOOL "Enable delayed assembly signing.")
if(DELAY_SIGN)
# The if-stmt converts On Y, YES to bool and from here on we want only a real
# true or false.
set(DELAY_SIGN "True")
list(APPEND CSBUILDFLAGS "/delaysign+" "/keyfile:${CMAKE_SOURCE_DIR}/public.snk")
else()
set(DELAY_SIGN "False")
endif(DELAY_SIGN)
# Suppress the warnings coming from the /doc switch. There is no point warning
# about missing documentation for certain methods. We all know about it :)
# 1591 - Missing documentation
# 1587 - XML comment not placed on a valid language element.
list(APPEND CSBUILDFLAGS "/nowarn:1591,1587")
# The lock is used to avoid regeneration of the sln-s and csproj-s if the
# build system is steered by another one. For example if TaiwuModsOpt is embedded
# as a vendor to say SolidReflector.
get_property(SLN_GEN_LOCK GLOBAL PROPERTY SLN_GEN_LOCK_PROPERTY)
if(NOT SLN_GEN_LOCK)
set_property(GLOBAL PROPERTY SLN_GEN_LOCK_PROPERTY "TaiwuModsOpt")
endif()
SET(DLLPATH "${CMAKE_CURRENT_LIST_DIR}/../dlls/")
SET(STEAMDIR "")
endif(UNIX)
include(CMakeModules/JSONParser.cmake)
#遍历Mod 目录
MACRO(SUBDIRLIST result curdir)
#message(STATUS "search sub dir in:" ${curdir})
FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
SET(dirlist "")
FOREACH(child ${children})
IF(IS_DIRECTORY ${curdir}/${child} AND (NOT ${child} STREQUAL "build") AND (NOT ${child} STREQUAL ".git") AND (NOT ${child} STREQUAL "CMakeModules") AND (NOT ${child} STREQUAL ".travis") AND (NOT ${child} STREQUAL "dlls") AND (EXISTS ${curdir}/${child}/Info.json))
message(STATUS "Found Mod: " ${child})
LIST(APPEND dirlist ${child})
ENDIF()
ENDFOREACH()
SET(${result} ${dirlist})
ENDMACRO()
if(UNIX)
#为每个目录生成Mod
MACRO(CREATEMOD moddir)
SET(FILELIST "")
#遍历所有.cs文件
FILE(GLOB_RECURSE FILES ${moddir}/*.cs)
FOREACH(csfile ${FILES})
LIST(APPEND FILELIST ${csfile})
ENDFOREACH()
#message(STATUS ${moddir} " file list:" ${FILELIST})
set(dlldepends "")
#遍历mod目录下dll,加入引用
FILE(GLOB_RECURSE MODDLLFILES ${moddir}/*.dll)
FOREACH(dllfile ${MODDLLFILES})
get_filename_component(dllfname ${dllfile} NAME)
string(REGEX REPLACE "\\.[^.]*$" "" dllname ${dllfname})
message(STATUS "Add dll type ${dllname} from ${dllfile} for mod ${moddir}")
LIST(APPEND dlldepends ${dllfile})
ENDFOREACH()
# 解析mod目录下 Info.json 文件, 生成对应 Properties/AssemblyInfo.cs ,添加mod依赖
file(STRINGS ${moddir}/Info.json infojson)
set(modinfo "")
set(DisplayName "")
set(Author "")
set(Version "")
set(Requirements "")
set(modinfo.Requirements "")
sbeParseJson(modinfo infojson)
# Now you can use parsed variables.
#foreach(var ${modinfo})
# message("vals : ${var} = ${${var}}")
#endforeach()
set(DisplayName ${modinfo.DisplayName})
set(Author ${modinfo.Author})
set(Version ${modinfo.Version})
set(Requirements ${modinfo.Requirements})
foreach(var ${Requirements})
string(REGEX REPLACE "-.*$" "" req ${modinfo.Requirements_${var}})
message(STATUS "requirements : ${modinfo.Requirements_${var}}")
LIST(APPEND dlldepends ${CMAKE_LIBRARY_OUTPUT_DIR}/${req}.dll)
endforeach()
#创建项目
csharp_add_library(
${moddir}.dll
A.cs
${FILELIST}
)
csharp_add_project_meta(
${moddir}.dll
"TargetFrameworkVersion" "2"
)
#项目基础引用
csharp_add_dependencies(
${moddir}.dll
System
System.Core
System.Data
System.Xml
System.Runtime.Serialization
${STEAMDIR}${DLLPATH}Assembly-CSharp.dll
${STEAMDIR}${DLLPATH}0Harmony12.dll
${STEAMDIR}${DLLPATH}Newtonsoft.Json.dll
${STEAMDIR}${DLLPATH}UnityEngine.dll
${STEAMDIR}${DLLPATH}UnityEngine.CoreModule.dll
${STEAMDIR}${DLLPATH}UnityEngine.ImageConversionModule.dll
${STEAMDIR}${DLLPATH}UnityEngine.TextRenderingModule.dll
${STEAMDIR}${DLLPATH}UnityEngine.IMGUIModule.dll
${STEAMDIR}${DLLPATH}UnityEngine.UI.dll
${STEAMDIR}${DLLPATH}UnityEngine.UIElementsModule.dll
${STEAMDIR}${DLLPATH}UnityEngine.UIModule.dll
${STEAMDIR}${DLLPATH}DOTween.dll
${dlldepends}
)
set_property(GLOBAL APPEND PROPERTY target_mod_version_property "${modinfo.Version}")
# When you are done, clean parsed variables
sbeClearJson(example)
ENDMACRO()
ENDIF(UNIX)
#win+vs
IF(WIN32)
#下载编译用dlls zip包
set(DLLPACK_VERSION "200319")
set(DLLPACK_NAME "dlls")
set(DLLPACK_DIR "${CMAKE_CURRENT_BINARY_DIR}\\${DLLPACK_NAME}")
set(DLLPACK_URL "https://github.com/phorcys/Taiwu_mods/releases/download/dll${DLLPACK_VERSION}/dlls-${DLLPACK_VERSION}.zip")
set(DLLPACKFILE "${CMAKE_CURRENT_BINARY_DIR}\\${DLLPACK_NAME}-${DLLPACK_VERSION}.zip")
set(timeout_args TIMEOUT 600)
set(timeout_msg "600 秒")
#移除已有
if(NOT EXISTS "${DLLPACKFILE}")
if(EXISTS "${DLLPACK_DIR}")
file(REMOVE_RECURSE "${DLLPACK_DIR}")
endif()
message(STATUS "下载 ${DLLPACKFILE} ... 自 ${DLLPACK_URL}
src='${DLLPACK_URL}'
dst='${DLLPACKFILE}'
timeout='下载超时 ${timeout_msg}'")
file(DOWNLOAD ${DLLPACK_URL} ${DLLPACKFILE}
${timeout_args}
${hash_args}
STATUS status
LOG log
SHOW_PROGRESS)
list(GET status 0 status_code)
list(GET status 1 status_string)
if(NOT status_code EQUAL 0)
message(FATAL_ERROR "错误: 下载 '${DLLPACK_URL}' 失败..
错误码 : ${status_code}
错误信息 : ${status_string}
日志 : ${log}
")
endif()
endif()
if (EXISTS "${DLLPACKFILE}" AND NOT EXISTS "${DLLPACK_DIR}")
message(STATUS "正在解压 ...
文件 '${DLLPACKFILE}'
到 '${DLLPACK_DIR}'")
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xvfz ${DLLPACKFILE}
WORKING_DIRECTORY "${DLLPACK_DIR}\\..\\"
RESULT_VARIABLE rv)
if(NOT rv EQUAL 0)
message(STATUS "解压错误,清理临时目录...")
file(REMOVE_RECURSE "${DLLPACK_DIR}")
message(FATAL_ERROR "解压错误: 解压 of '${DLLPACKFILE}' 失败")
endif()
endif()
#为每个目录生成Mod
MACRO(CREATEMOD moddir)
# 解析mod目录下 Info.json 文件, 生成对应 Properties/AssemblyInfo.cs ,添加mod依赖
#读取文件
file(STRINGS ${moddir}/Info.json infojson)
#初始化变量
set(modinfo "")
set(Id "")
set(DisplayName "")
set(Author "")
set(Version "")
set(Requirements "")
set(modinfo.Requirements "")
#解析json
sbeParseJson(modinfo infojson)
# 调试代码:列出json值
#foreach(var ${modinfo})
# message("vals : ${var} = ${${var}}")
#endforeach()
#取json值
set(Id ${modinfo.Id})
set(DisplayName ${modinfo.DisplayName})
set(Author ${modinfo.Author})
set(Version ${modinfo.Version})
set(Requirements ${modinfo.Requirements})
message(STATUS "Found Mod: ${Id} ${DisplayName} Author ${Author} Version ${Version} ${CMAKE_CURRENT_BINARY_DIR} ")
SET(FILELIST "")
#遍历所有.cs文件
FILE(GLOB_RECURSE FILES ${moddir}/*.cs)
FOREACH(csfile ${FILES})
LIST(APPEND FILELIST ${csfile})
ENDFOREACH()
set(assemblyinfo "using System\;
using System.Diagnostics\;
using System.Reflection\;
using System.Runtime.CompilerServices\;
using System.Runtime.InteropServices\;
using System.Runtime.Versioning\;
[assembly: AssemblyVersion(\"${Version}\")]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyTitle(\"${Id}\")]
[assembly: AssemblyDescription(\"${DisplayName}\")]
[assembly: AssemblyCompany(\"TaiwuMods\")]
[assembly: AssemblyProduct(\"${Id}\")]
[assembly: AssemblyCopyright(\"Copyright © 2018\")]
[assembly: AssemblyFileVersion(\"${Version}\")]
")
FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${moddir}/Properties/AssemblyInfo.cs" ${assemblyinfo})
LIST(APPEND FILELIST "${CMAKE_CURRENT_BINARY_DIR}/${moddir}/Properties/AssemblyInfo.cs")
#message(STATUS ${moddir} " file list:" ${FILELIST})
#创建项目
add_library(${moddir}
SHARED
${FILELIST}
${moddir}/Info.json
)
#项目版本 .net 4.7.2
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.7.2")
#项目基础引用
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCES
"System"
"System.Core"
"System.Data"
"System.Xml"
"System.Runtime.Serialization"
)
#AssemblyInfo.cs 处理
csharp_set_designer_cs_properties(
"${CMAKE_CURRENT_BINARY_DIR}/${moddir}/Properties/AssemblyInfo.cs"
)
#dll 引用
#游戏主dll
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_Assembly-CSharp "${DLLPACK_DIR}\\Assembly-CSharp.dll")
# harmonypatch工具
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_Harmony12 "${DLLPACK_DIR}\\0harmony12.dll")
#其他游戏相关dll
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_Newtonsoft.Json "${DLLPACK_DIR}\\Newtonsoft.Json.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine "${DLLPACK_DIR}\\UnityEngine.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.CoreModule "${DLLPACK_DIR}\\UnityEngine.CoreModule.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.ImageConversionModule "${DLLPACK_DIR}\\UnityEngine.ImageConversionModule.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.TextRenderingModule "${DLLPACK_DIR}\\UnityEngine.TextRenderingModule.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.IMGUIModule "${DLLPACK_DIR}\\UnityEngine.IMGUIModule.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.UI "${DLLPACK_DIR}\\UnityEngine.UI.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.UIElementsModule "${DLLPACK_DIR}\\UnityEngine.UIElementsModule.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.UIModule "${DLLPACK_DIR}\\UnityEngine.UIModule.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_DOTween "${DLLPACK_DIR}\\DOTween.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.PhysicsModule "${DLLPACK_DIR}\\UnityEngine.PhysicsModule.dll")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_UnityEngine.Physics2DModule "${DLLPACK_DIR}\\UnityEngine.Physics2DModule.dll")
#遍历mod目录下dll,加入引用
FILE(GLOB_RECURSE MODDLLFILES ${moddir}/*.dll)
FOREACH(dllfile ${MODDLLFILES})
get_filename_component(dllfname ${dllfile} NAME)
string(REGEX REPLACE "\\.[^.]*$" "" dllname ${dllfname})
message(STATUS " Add dll type ${dllname} from ${dllfile} for mod ${moddir}")
set_property(TARGET ${moddir} PROPERTY VS_DOTNET_REFERENCE_${dllname} "${dllfile}")
ENDFOREACH()
#设置项目引用
set(ProjectDeps "")
set(var "")
foreach(var ${Requirements})
string(REGEX REPLACE "-.*$" "" req ${modinfo.Requirements_${var}})
message(STATUS " Mod requirements : ${modinfo.Requirements_${var}}")
target_link_libraries(${moddir} ${req})
endforeach()
#清除json
sbeClearJson(example)
#生成后事件,copy dll到游戏Mods目录
ADD_CUSTOM_COMMAND(TARGET ${moddir}
POST_BUILD
COMMAND if not exist "\"${STEAMDIR}\\Mods\\${moddir}\\\"" echo not copy file & goto END
COMMAND copy "\"$(TargetPATH)\" \"${STEAMDIR}\\Mods\\${moddir}\""
COMMAND "..\\..\\CopyMod.exe" ${moddir} "\"${STEAMDIR}\\Mods\""
COMMAND echo "copy dll file ${moddir}.dll ok"
COMMENT "copy dll file to steam game"
COMMAND :END
)
ENDMACRO()
ENDIF(WIN32)
message(STATUS "Finding All subdir that contains mods")
SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_LIST_DIR})
#message(STATUS "subdir list:" ${SUBDIRS})
FOREACH(subdir ${SUBDIRS})
CREATEMOD(${subdir})
ENDFOREACH()
IF(UNIX)
# Resolve all target dependencies
csharp_resolve_dependencies()
get_property(SLN_GEN_LOCK GLOBAL PROPERTY SLN_GEN_LOCK_PROPERTY)
if (SLN_GEN_LOCK STREQUAL "TaiwuModsOpt")
# Save solution
csharp_save_vs_solution(TaiwuModsOpt)
endif()
ENDIF(UNIX)