├── .gitattributes ├── .gitignore ├── README.md ├── cpptest ├── config │ └── Achive.txt ├── cpptest.vcxproj ├── cpptest.vcxproj.filters ├── main.cpp ├── tableconfig.cpp ├── tableconfig.h ├── tablestruct.h └── tabletool │ ├── myconfig.h │ ├── readtablefield.h │ ├── readtablefile.cpp │ ├── readtablefile.h │ └── singleton.h ├── csharptest ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── TableConfig.cs ├── TableStruct.cs ├── config │ └── Achive.txt ├── csharptest.csproj └── tabtool │ ├── MyConfig.cs │ ├── ReadTable.cs │ ├── SingletonTable.cs │ └── TableManager.cs ├── tabtool.sln ├── tabtool ├── App.config ├── CmdlineHelper.cs ├── CodeGen.cs ├── ExcelHelper.cs ├── NPOI │ ├── ICSharpCode.SharpZipLib.dll │ ├── NPOI.OOXML.dll │ ├── NPOI.OpenXml4Net.dll │ ├── NPOI.OpenXmlFormats.dll │ └── NPOI.dll ├── Program.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── TableMeta.cs ├── TableStruct.cs └── tabtool.csproj └── test ├── Achive.xlsx ├── meta.tbs └── 一键导表.bat /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | [Xx]64/ 19 | [Xx]86/ 20 | [Bb]uild/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | 85 | # Visual Studio profiler 86 | *.psess 87 | *.vsp 88 | *.vspx 89 | *.sap 90 | 91 | # TFS 2012 Local Workspace 92 | $tf/ 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | *.DotSettings.user 101 | 102 | # JustCode is a .NET coding add-in 103 | .JustCode 104 | 105 | # TeamCity is a build add-in 106 | _TeamCity* 107 | 108 | # DotCover is a Code Coverage Tool 109 | *.dotCover 110 | 111 | # NCrunch 112 | _NCrunch_* 113 | .*crunch*.local.xml 114 | nCrunchTemp_* 115 | 116 | # MightyMoose 117 | *.mm.* 118 | AutoTest.Net/ 119 | 120 | # Web workbench (sass) 121 | .sass-cache/ 122 | 123 | # Installshield output folder 124 | [Ee]xpress/ 125 | 126 | # DocProject is a documentation generator add-in 127 | DocProject/buildhelp/ 128 | DocProject/Help/*.HxT 129 | DocProject/Help/*.HxC 130 | DocProject/Help/*.hhc 131 | DocProject/Help/*.hhk 132 | DocProject/Help/*.hhp 133 | DocProject/Help/Html2 134 | DocProject/Help/html 135 | 136 | # Click-Once directory 137 | publish/ 138 | 139 | # Publish Web Output 140 | *.[Pp]ublish.xml 141 | *.azurePubxml 142 | 143 | # TODO: Un-comment the next line if you do not want to checkin 144 | # your web deploy settings because they may include unencrypted 145 | # passwords 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # NuGet Packages 150 | *.nupkg 151 | # The packages folder can be ignored because of Package Restore 152 | **/packages/* 153 | # except build/, which is used as an MSBuild target. 154 | !**/packages/build/ 155 | # Uncomment if necessary however generally it will be regenerated when needed 156 | #!**/packages/repositories.config 157 | # NuGet v3's project.json files produces more ignoreable files 158 | *.nuget.props 159 | *.nuget.targets 160 | 161 | # Microsoft Azure Build Output 162 | csx/ 163 | *.build.csdef 164 | 165 | # Microsoft Azure Emulator 166 | ecf/ 167 | rcf/ 168 | 169 | # Microsoft Azure ApplicationInsights config file 170 | ApplicationInsights.config 171 | 172 | # Windows Store app package directory 173 | AppPackages/ 174 | BundleArtifacts/ 175 | 176 | # Visual Studio cache files 177 | # files ending in .cache can be ignored 178 | *.[Cc]ache 179 | # but keep track of directories ending in .cache 180 | !*.[Cc]ache/ 181 | 182 | # Others 183 | ClientBin/ 184 | [Ss]tyle[Cc]op.* 185 | ~$* 186 | *~ 187 | *.dbmdl 188 | *.dbproj.schemaview 189 | *.pfx 190 | *.publishsettings 191 | node_modules/ 192 | orleans.codegen.cs 193 | 194 | # RIA/Silverlight projects 195 | Generated_Code/ 196 | 197 | # Backup & report files from converting an old project file 198 | # to a newer Visual Studio version. Backup files are not needed, 199 | # because we have git ;-) 200 | _UpgradeReport_Files/ 201 | Backup*/ 202 | UpgradeLog*.XML 203 | UpgradeLog*.htm 204 | 205 | # SQL Server files 206 | *.mdf 207 | *.ldf 208 | 209 | # Business Intelligence projects 210 | *.rdl.data 211 | *.bim.layout 212 | *.bim_*.settings 213 | 214 | # Microsoft Fakes 215 | FakesAssemblies/ 216 | 217 | # GhostDoc plugin setting file 218 | *.GhostDoc.xml 219 | 220 | # Node.js Tools for Visual Studio 221 | .ntvs_analysis.dat 222 | 223 | # Visual Studio 6 build log 224 | *.plg 225 | 226 | # Visual Studio 6 workspace options file 227 | *.opt 228 | 229 | # Visual Studio LightSwitch build output 230 | **/*.HTMLClient/GeneratedArtifacts 231 | **/*.DesktopClient/GeneratedArtifacts 232 | **/*.DesktopClient/ModelManifest.xml 233 | **/*.Server/GeneratedArtifacts 234 | **/*.Server/ModelManifest.xml 235 | _Pvt_Extensions 236 | 237 | # LightSwitch generated files 238 | GeneratedArtifacts/ 239 | ModelManifest.xml 240 | 241 | # Paket dependency manager 242 | .paket/paket.exe 243 | 244 | # FAKE - F# Make 245 | .fake/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tabtool 2 | 导表工具,excel表格导出csv配置文件并生成C++\C#代码解析配表 3 | 4 | ## 推荐工作流: 5 | 策划案--解决方案--前后端配置需求汇总--表格版本--打表工具--配置文件--代码生成。 6 | 7 | ## 命名规则。 8 | 1. 驼峰命名。 9 | 2. 表格命名:系统名+表名.xsl 10 | 3. 字段命名:使用通用的单词 id type count 等 11 | 12 | ## excel表规则。 13 | -    第一行注释,给策划看,也会在生成代码中作为注释 14 | -    第二行name,也是生成代码中的结构体字段名称 15 | -    第三行filter,"client"标识该字段只导出到客户端配置文件,"server"标识只导出到服务器配置文件,"all"标识前后端都需要,"not"标识不导出该字段。 16 | -    第四行type,参考下面的字段类型说明。 17 | -    每个表第一个字段必须是id字段,id必须从1开始,0是读表错误。 18 | -    id字段的filter如果标识为"client"则表示这个表只导出客户端配置文件,不导出服务器配表。反之亦然。 19 | 20 | ## 字段类型 21 | 22 | -   int 整数和bool 23 | -    float 浮点数 24 | -    string 字符串 25 | -    int+ 整数迭代 26 | -    float+ 浮点数迭代 27 | -    string+ 字符串迭代 28 | -    tbsIdCount 定义在meta.tbs中的结构体 29 | -    tbsIdCount+ 结构体迭代 30 |     31 | ## 复合字段及其迭代 32 | - 一级字段迭代:`11,22,33,44`在type中用`int+`表示。 33 | - 二级字段迭代:`1,1;2,2`在type中用`tbsIdCount+`表示。 34 | - 通过一个结构描述文件支持结构体,`meta.tbs`。 35 | - 我认为表字段结构体嵌套是没有意义的,所以仅支持到二级复合字段。 36 | - 注意excel中填写,时要设置单元格为文本模式,否则会变成数字分隔符。 37 | - tbs文件非常简单,如下就定义一个结构体tbsIdCount: 38 | 39 | ```c 40 | //表示id和数量 41 | tbsIdCount { 42 | id int 43 | count int 44 | } 45 | ``` 46 | 47 | ## 代码生成 48 | - C++版本 tbs文件tablestruct.h csv文件生成生成一对tableconfig.h/tableconfig.cpp 49 | - C#版本  tbs文件生成TableStruct.cs csv文件生成TableConfig.cs 50 | - Go版本  TODO 暂时没用到,用到了再支持 51 | 52 | ## 错误检查 53 |    类型模式不匹配的字段会在打表过程中检查出来。 54 | ## 导表工具使用 55 |    参考test目录中`一键导出表.bat`的用法。 56 | ``` 57 | "../tabtool/bin/Debug/tabtool.exe" --out_client ../csharptest/config/ --out_server ../cpptest/config/ --out_cpp ../cpptest/ --out_cs ../csharptest/ --in_excel ./ --in_tbs ./meta.tbs 58 | ```--out_client 指定导出客户端导出配置文件目录 59 |   --out_server 导出服务器配置文件目录 60 |   --out_cpp 导出C++代码目录,可选 61 |   --out_cs 导出C#代码目录,可选 62 |   --in_excel excel文件所在的目录 63 |   --in_tbs tbs文件路径(表中用到的结构体) 64 | 65 | ## C++使用 66 | 67 | 68 | -------------------------------------------------------------------------------- /cpptest/config/Achive.txt: -------------------------------------------------------------------------------- 1 | id line test1 test2 test3 2 | 1 1 1,100 -1,2,-3 1,1;2,2 3 | 2 1 1,1000 1,2,3,4 1,1;2,2;3,3 4 | -------------------------------------------------------------------------------- /cpptest/cpptest.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {5AF57802-51EF-4574-BC38-4475B4F556A1} 23 | Win32Proj 24 | cpptest 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | bin 75 | 76 | 77 | true 78 | 79 | 80 | false 81 | 82 | 83 | false 84 | 85 | 86 | 87 | 88 | 89 | Level3 90 | Disabled 91 | _CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | 98 | 99 | 100 | 101 | 102 | 103 | Level3 104 | Disabled 105 | _CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 106 | true 107 | 108 | 109 | Console 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | 117 | 118 | MaxSpeed 119 | true 120 | true 121 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 122 | true 123 | 124 | 125 | Console 126 | true 127 | true 128 | true 129 | 130 | 131 | 132 | 133 | Level3 134 | 135 | 136 | MaxSpeed 137 | true 138 | true 139 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 140 | true 141 | 142 | 143 | Console 144 | true 145 | true 146 | true 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /cpptest/cpptest.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | 14 | 15 | Source Files 16 | 17 | 18 | tabletool 19 | 20 | 21 | Source Files 22 | 23 | 24 | 25 | 26 | tabletool 27 | 28 | 29 | tabletool 30 | 31 | 32 | tabletool 33 | 34 | 35 | tabletool 36 | 37 | 38 | Source Files 39 | 40 | 41 | Source Files 42 | 43 | 44 | -------------------------------------------------------------------------------- /cpptest/main.cpp: -------------------------------------------------------------------------------- 1 | #include "tableconfig.h" 2 | #include 3 | 4 | int main() 5 | { 6 | if (LoadTableConfig()) 7 | { 8 | auto item = Singleton::Instance()->GetTableItem(1); 9 | if (item != nullptr) 10 | { 11 | std::cout << item->test3[1].value << std::endl; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /cpptest/tableconfig.cpp: -------------------------------------------------------------------------------- 1 | //THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT! 2 | //GENERATE TIME [2017/2/28 11:08:53] 3 | #include "tableconfig.h" 4 | 5 | bool LoadTableConfig() 6 | { 7 | stConfigScope scope; 8 | if(false == Singleton::Instance()->Load()) 9 | { 10 | ErrorLog("加载配置%s出错",Singleton::Instance()->GetTableFile().c_str() ); 11 | return false; 12 | } 13 | return true; 14 | } 15 | 16 | bool cfgAchiveTable::Load() 17 | { 18 | ReadTableFile reader; 19 | reader.Initialize(); 20 | 21 | if (!reader.Init(GetTableFile().c_str())) 22 | return false; 23 | 24 | DataReader dr; 25 | int iRows = reader.GetRowCount(); 26 | int iCols = reader.GetColCount(); 27 | 28 | for (int i = 1; i < iRows; ++i) 29 | { 30 | tbsAchiveItem item; 31 | item.id = atoi(reader.GetValue(i, "id")); 32 | item.line = atoi(reader.GetValue(i, "line")); 33 | item.test1 = dr.GetObject(reader.GetValue(i, "test1")); 34 | item.test2 = dr.GetIntList(reader.GetValue(i, "test2")); 35 | item.test3 = dr.GetObjectList(reader.GetValue(i, "test3")); 36 | m_Items[item.id] = item; 37 | } 38 | 39 | return true; 40 | } 41 | -------------------------------------------------------------------------------- /cpptest/tableconfig.h: -------------------------------------------------------------------------------- 1 | //THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT! 2 | //GENERATE TIME [2017/2/28 11:08:53] 3 | #pragma once 4 | # include "tabletool/myconfig.h" 5 | # include "tabletool/singleton.h" 6 | # include "tabletool/readtablefield.h" 7 | # include "tablestruct.h" 8 | 9 | 10 | bool LoadTableConfig(); 11 | 12 | 13 | struct tbsAchiveItem { 14 | int id; 15 | int line; 16 | tbsIdCount test1; 17 | vector test2; 18 | vector test3; 19 | }; 20 | 21 | class cfgAchiveTable:public IConfigTable{ 22 | public: 23 | virtual bool Load(); 24 | 25 | string GetTableFile() 26 | { 27 | string f = WORK_DIR; 28 | f = f + "Achive.txt"; 29 | return f; 30 | } 31 | 32 | }; 33 | 34 | -------------------------------------------------------------------------------- /cpptest/tablestruct.h: -------------------------------------------------------------------------------- 1 | //THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT! 2 | //GENERATE TIME [2017/2/28 11:08:53] 3 | #pragma once 4 | #include "tabletool/readtablefield.h" 5 | #include "tabletool/myconfig.h" 6 | 7 | 8 | struct tbsIdCount : public ITableObject 9 | { 10 | int id; 11 | int count; 12 | 13 | virtual bool FromString(string s) 14 | { 15 | DataReader dr; 16 | vector vs = dr.GetStringList(s,','); 17 | if (vs.size() != 2) 18 | { 19 | ErrorLog("tbsIdCount字段配置错误"); 20 | return false; 21 | } 22 | id = stoi(vs[0]); 23 | count = stoi(vs[1]); 24 | return true; 25 | } 26 | }; 27 | 28 | 29 | struct tbsKeyValue : public ITableObject 30 | { 31 | int key; 32 | int value; 33 | 34 | virtual bool FromString(string s) 35 | { 36 | DataReader dr; 37 | vector vs = dr.GetStringList(s,','); 38 | if (vs.size() != 2) 39 | { 40 | ErrorLog("tbsKeyValue字段配置错误"); 41 | return false; 42 | } 43 | key = stoi(vs[0]); 44 | value = stoi(vs[1]); 45 | return true; 46 | } 47 | }; 48 | 49 | 50 | struct tbsTest : public ITableObject 51 | { 52 | int a; 53 | string b; 54 | float c; 55 | 56 | virtual bool FromString(string s) 57 | { 58 | DataReader dr; 59 | vector vs = dr.GetStringList(s,','); 60 | if (vs.size() != 3) 61 | { 62 | ErrorLog("tbsTest字段配置错误"); 63 | return false; 64 | } 65 | a = stoi(vs[0]); 66 | b = (vs[1]); 67 | c = stof(vs[2]); 68 | return true; 69 | } 70 | }; 71 | 72 | -------------------------------------------------------------------------------- /cpptest/tabletool/myconfig.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/cpptest/tabletool/myconfig.h -------------------------------------------------------------------------------- /cpptest/tabletool/readtablefield.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/cpptest/tabletool/readtablefield.h -------------------------------------------------------------------------------- /cpptest/tabletool/readtablefile.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/cpptest/tabletool/readtablefile.cpp -------------------------------------------------------------------------------- /cpptest/tabletool/readtablefile.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "myconfig.h" 7 | 8 | class ReadTableFile 9 | { 10 | public: 11 | ReadTableFile(); 12 | ~ReadTableFile(); 13 | 14 | static void Initialize(); 15 | static void Destroy(); 16 | 17 | bool Init(const char* szFileName); 18 | bool Init(const char* pBuffer, int iLength, const char* szFileName = NULL); 19 | int GetRowCount(); 20 | int GetColCount(); 21 | const char* GetValue(int nRow,const char* szName); 22 | const char* GetValue(int nRow, int nCol); 23 | const char* GetFileName()const{return m_szFileName;} 24 | 25 | private: 26 | 27 | static std::vector* m_pVects; 28 | static char* m_pszReadBuf; 29 | std::map m_mNamePos; 30 | int m_nLeftBufLen; 31 | char* m_pszReadPtr; 32 | FILE* m_fpConfigFile; 33 | static int m_nRowCnt; 34 | static int m_nColCnt; 35 | char m_szFileName[1024]; 36 | }; 37 | 38 | struct stConfigScope 39 | { 40 | public: 41 | stConfigScope(){ 42 | ReadTableFile::Initialize(); 43 | } 44 | ~stConfigScope(){ 45 | ReadTableFile::Destroy(); 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /cpptest/tabletool/singleton.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/cpptest/tabletool/singleton.h -------------------------------------------------------------------------------- /csharptest/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /csharptest/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using tabtool; 7 | 8 | namespace csharptest 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | if(TableConfig.Instance.LoadTableConfig()) 15 | { 16 | tbsAchiveItem item = cfgAchiveTable.Instance.GetTableItem(1); 17 | if(item != null) 18 | { 19 | Console.WriteLine(item.name); 20 | } 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /csharptest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("csharptest")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("csharptest")] 13 | [assembly: AssemblyCopyright("Copyright © 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("838ba18b-7ef9-4341-b319-cb58f9f451ac")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /csharptest/TableConfig.cs: -------------------------------------------------------------------------------- 1 | //THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT! 2 | //GENERATE TIME [2017/2/28 11:08:53] 3 | 4 | using System.Collections; 5 | using System.Collections.Generic; 6 | using System.Data; 7 | 8 | namespace tabtool 9 | { 10 | 11 | public class tbsAchiveItem 12 | { 13 | public int id; 14 | public string name; 15 | public tbsIdCount test1; 16 | public List test2; 17 | public List test3; 18 | } 19 | 20 | public class cfgAchiveTable : TableManager 21 | { 22 | public override bool Load() 23 | { 24 | TableReader tr = new TableReader(); 25 | DataReader dr = new DataReader(); 26 | DataTable dt = tr.ReadFile(MyConfig.WorkDir+"Achive.txt"); 27 | 28 | foreach(DataRow row in dt.Rows) 29 | { 30 | var item = new tbsAchiveItem(); 31 | item.id = dr.GetInt(row["id"].ToString()); 32 | item.name = (row["name"].ToString()); 33 | item.test1 = dr.GetObject(row["test1"].ToString()); 34 | item.test2 = dr.GetIntList(row["test2"].ToString()); 35 | item.test3 = dr.GetObjectList(row["test3"].ToString()); 36 | m_Items[item.id] = item; 37 | } 38 | return true; 39 | } 40 | } 41 | 42 | public class TableConfig : SingletonTable 43 | { 44 | public bool LoadTableConfig() 45 | { 46 | if (!cfgAchiveTable.Instance.Load()) return false; 47 | return true; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /csharptest/TableStruct.cs: -------------------------------------------------------------------------------- 1 | //THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT! 2 | //GENERATE TIME [2017/2/28 11:08:53] 3 | 4 | namespace tabtool 5 | { 6 | public class tbsIdCount : ITableObject 7 | { 8 | int id; 9 | int count; 10 | public bool FromString(string s) 11 | { 12 | DataReader dr = new DataReader(); 13 | 14 | var vs = s.Split(','); 15 | if (vs.Length != 2) 16 | { 17 | return false; 18 | } 19 | 20 | id = dr.GetInt(vs[0]); 21 | count = dr.GetInt(vs[1]); 22 | return true; 23 | } 24 | }; 25 | 26 | public class tbsKeyValue : ITableObject 27 | { 28 | int key; 29 | int value; 30 | public bool FromString(string s) 31 | { 32 | DataReader dr = new DataReader(); 33 | 34 | var vs = s.Split(','); 35 | if (vs.Length != 2) 36 | { 37 | return false; 38 | } 39 | 40 | key = dr.GetInt(vs[0]); 41 | value = dr.GetInt(vs[1]); 42 | return true; 43 | } 44 | }; 45 | 46 | public class tbsTest : ITableObject 47 | { 48 | int a; 49 | string b; 50 | float c; 51 | public bool FromString(string s) 52 | { 53 | DataReader dr = new DataReader(); 54 | 55 | var vs = s.Split(','); 56 | if (vs.Length != 2) 57 | { 58 | return false; 59 | } 60 | 61 | a = dr.GetInt(vs[0]); 62 | b = vs[1]; 63 | c = dr.GetFloat(vs[2]); 64 | return true; 65 | } 66 | }; 67 | 68 | } 69 | -------------------------------------------------------------------------------- /csharptest/config/Achive.txt: -------------------------------------------------------------------------------- 1 | id name test1 test2 test3 2 | 1 消灭100个敌人 1,100 -1,2,-3 1,1;2,2 3 | 2 消灭1000个敌人 1,1000 1,2,3,4 1,1;2,2;3,3 4 | -------------------------------------------------------------------------------- /csharptest/csharptest.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {838BA18B-7EF9-4341-B319-CB58F9F451AC} 8 | Exe 9 | Properties 10 | csharptest 11 | csharptest 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 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 | 67 | -------------------------------------------------------------------------------- /csharptest/tabtool/MyConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace tabtool 8 | { 9 | class MyConfig 10 | { 11 | public const string WorkDir = "../../config/"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /csharptest/tabtool/ReadTable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data; 4 | using System.IO; 5 | 6 | namespace tabtool 7 | { 8 | //tbs中的结构体都从这里继承 9 | interface ITableObject 10 | { 11 | bool FromString(string s); 12 | } 13 | 14 | //表字段读取 15 | class DataReader 16 | { 17 | public List GetStringList(string s, char delim) 18 | { 19 | string[] t = s.Split(delim); 20 | List ret = new List(); 21 | ret.AddRange(t); 22 | return ret; 23 | } 24 | 25 | public int GetInt(string s) 26 | { 27 | return int.Parse(s); 28 | } 29 | 30 | public List GetIntList(string s) 31 | { 32 | string[] vs = s.Split(','); 33 | List ret = new List(); 34 | foreach (var ss in vs) 35 | { 36 | int x = int.Parse(ss); 37 | ret.Add(x); 38 | } 39 | return ret; 40 | } 41 | 42 | public float GetFloat(string s) 43 | { 44 | return float.Parse(s); 45 | } 46 | 47 | public List GetFloatList(string s) 48 | { 49 | string[] vs = s.Split(','); 50 | List ret = new List(); 51 | foreach (var ss in vs) 52 | { 53 | float x = float.Parse(ss); 54 | ret.Add(x); 55 | } 56 | return ret; 57 | } 58 | 59 | public T GetObject(string s) where T : ITableObject, new() 60 | { 61 | T obj = new T(); 62 | obj.FromString(s); 63 | return obj; 64 | } 65 | 66 | public List GetObjectList(string s) where T : ITableObject, new() 67 | { 68 | string[] vs = s.Split(';'); 69 | List ret = new List(); 70 | foreach (var ss in vs) 71 | { 72 | ret.Add(GetObject(ss)); 73 | } 74 | return ret; 75 | } 76 | }; 77 | 78 | class TableReader 79 | { 80 | public DataTable ReadFile(string filepath) 81 | { 82 | DataTable dt = new DataTable(); 83 | //首行是字段名 之后是字段值 84 | string[] lines = File.ReadAllLines(filepath); 85 | bool firstline = true; 86 | foreach (var line in lines) 87 | { 88 | string[] words = line.Split('\t'); 89 | if (words == null || words.Length == 0) 90 | { 91 | continue; 92 | } 93 | if (firstline) 94 | { 95 | firstline = false; 96 | foreach (var word in words) 97 | { 98 | dt.Columns.Add(word); 99 | } 100 | continue; 101 | } 102 | DataRow row = dt.NewRow(); 103 | int i = 0; 104 | foreach (var word in words) 105 | { 106 | row[i++] = word; 107 | } 108 | dt.Rows.Add(row); 109 | } 110 | return dt; 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /csharptest/tabtool/SingletonTable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class SingletonTable 4 | { 5 | protected static readonly T ms_instance = Activator.CreateInstance(); 6 | public static T Instance { get { return ms_instance; } } 7 | protected SingletonTable() { } 8 | } 9 | -------------------------------------------------------------------------------- /csharptest/tabtool/TableManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Text; 6 | 7 | namespace tabtool 8 | { 9 | public abstract class TableManager : SingletonTable 10 | { 11 | protected Dictionary m_Items = new Dictionary(); 12 | 13 | public Dictionary GetTable() 14 | { 15 | return m_Items; 16 | } 17 | 18 | public T GetTableItem(int key) 19 | { 20 | T t; 21 | if (m_Items.TryGetValue(key, out t)) 22 | { 23 | return t; 24 | } 25 | return default(T); 26 | } 27 | 28 | 29 | public abstract bool Load(); 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /tabtool.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25123.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tabtool", "tabtool\tabtool.csproj", "{E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csharptest", "csharptest\csharptest.csproj", "{838BA18B-7EF9-4341-B319-CB58F9F451AC}" 9 | EndProject 10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cpptest", "cpptest\cpptest.vcxproj", "{5AF57802-51EF-4574-BC38-4475B4F556A1}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Debug|x64 = Debug|x64 16 | Debug|x86 = Debug|x86 17 | Release|Any CPU = Release|Any CPU 18 | Release|x64 = Release|x64 19 | Release|x86 = Release|x86 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Debug|x64.ActiveCfg = Debug|Any CPU 25 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Debug|x64.Build.0 = Debug|Any CPU 26 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Debug|x86.ActiveCfg = Debug|Any CPU 27 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Debug|x86.Build.0 = Debug|Any CPU 28 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Release|x64.ActiveCfg = Release|Any CPU 31 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Release|x64.Build.0 = Release|Any CPU 32 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Release|x86.ActiveCfg = Release|Any CPU 33 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7}.Release|x86.Build.0 = Release|Any CPU 34 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Debug|x64.ActiveCfg = Debug|Any CPU 37 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Debug|x64.Build.0 = Debug|Any CPU 38 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Debug|x86.ActiveCfg = Debug|Any CPU 39 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Debug|x86.Build.0 = Debug|Any CPU 40 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Release|Any CPU.Build.0 = Release|Any CPU 42 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Release|x64.ActiveCfg = Release|Any CPU 43 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Release|x64.Build.0 = Release|Any CPU 44 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Release|x86.ActiveCfg = Release|Any CPU 45 | {838BA18B-7EF9-4341-B319-CB58F9F451AC}.Release|x86.Build.0 = Release|Any CPU 46 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Debug|Any CPU.ActiveCfg = Debug|Win32 47 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Debug|x64.ActiveCfg = Debug|x64 48 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Debug|x64.Build.0 = Debug|x64 49 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Debug|x86.ActiveCfg = Debug|Win32 50 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Debug|x86.Build.0 = Debug|Win32 51 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Release|Any CPU.ActiveCfg = Release|Win32 52 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Release|x64.ActiveCfg = Release|x64 53 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Release|x64.Build.0 = Release|x64 54 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Release|x86.ActiveCfg = Release|Win32 55 | {5AF57802-51EF-4574-BC38-4475B4F556A1}.Release|x86.Build.0 = Release|Win32 56 | EndGlobalSection 57 | GlobalSection(SolutionProperties) = preSolution 58 | HideSolutionNode = FALSE 59 | EndGlobalSection 60 | EndGlobal 61 | -------------------------------------------------------------------------------- /tabtool/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /tabtool/CmdlineHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace tabtool 8 | { 9 | class CmdlineHelper 10 | { 11 | public CmdlineHelper(string[] args) 12 | { 13 | m_args = args; 14 | } 15 | 16 | string[] m_args; 17 | 18 | public bool Has(string s) 19 | { 20 | return m_args.Count(p => p == s) > 0; 21 | } 22 | 23 | public string Get(string s) 24 | { 25 | for(int i = 0; i < m_args.Count(); i++) 26 | { 27 | if (m_args[i] == s && i + 1 < m_args.Count()) 28 | { 29 | return m_args[i + 1]; 30 | } 31 | } 32 | return null; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tabtool/CodeGen.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.IO; 7 | 8 | namespace tabtool 9 | { 10 | class CodeGen 11 | { 12 | public static void MakeCppFile(List metalist, string codepath) 13 | { 14 | string hfile = codepath + "tableconfig.h"; 15 | using (FileStream fs = new FileStream(hfile, FileMode.Create, FileAccess.Write)) 16 | { 17 | using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) 18 | { 19 | sw.WriteLine("//THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT!"); 20 | sw.WriteLine("//GENERATE TIME [{0}]", System.DateTime.Now.ToString()); 21 | sw.WriteLine("#pragma once"); 22 | sw.WriteLine("# include \"tabletool/myconfig.h\""); 23 | sw.WriteLine("# include \"tabletool/singleton.h\""); 24 | sw.WriteLine("# include \"tabletool/readtablefield.h\""); 25 | sw.WriteLine("# include \"tablestruct.h\""); 26 | sw.WriteLine(); 27 | sw.WriteLine(); 28 | sw.WriteLine("bool LoadTableConfig();"); 29 | sw.WriteLine(); 30 | 31 | foreach (var meta in metalist) 32 | { 33 | sw.WriteLine(); 34 | sw.WriteLine("struct {0} {{", meta.GetItemName()); 35 | foreach(var field in meta.Fields) 36 | { 37 | sw.WriteLine(" {0} {1};", field.GetCppTypeName(), field.fieldName); 38 | } 39 | sw.WriteLine("};"); 40 | sw.WriteLine(); 41 | sw.WriteLine("class {0}:public IConfigTable<{1}>{{", meta.GetClassName(), meta.GetItemName()); 42 | sw.WriteLine("public:"); 43 | sw.WriteLine(" virtual bool Load();"); 44 | sw.WriteLine(); 45 | sw.WriteLine(" string GetTableFile()"); 46 | sw.WriteLine(" {"); 47 | sw.WriteLine(" string f = WORK_DIR;"); 48 | sw.WriteLine(" f = f + \"{0}.txt\";", meta.TableName); 49 | sw.WriteLine(" return f;"); 50 | sw.WriteLine(" }"); 51 | sw.WriteLine(); 52 | sw.WriteLine("};"); 53 | sw.WriteLine(); 54 | } 55 | 56 | } 57 | } 58 | 59 | string cppfile = codepath + "tableconfig.cpp"; 60 | using (FileStream fs = new FileStream(cppfile, FileMode.Create, FileAccess.Write)) 61 | { 62 | using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) 63 | { 64 | sw.WriteLine("//THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT!"); 65 | sw.WriteLine("//GENERATE TIME [{0}]", System.DateTime.Now.ToString()); 66 | sw.WriteLine("#include \"tableconfig.h\""); 67 | sw.WriteLine(); 68 | sw.WriteLine("bool LoadTableConfig()"); 69 | sw.WriteLine("{"); 70 | sw.WriteLine(" stConfigScope scope;"); 71 | foreach (var meta in metalist) 72 | { 73 | sw.WriteLine(" if(false == Singleton<{0}>::Instance()->Load())", meta.GetClassName()); 74 | sw.WriteLine(" {"); 75 | sw.WriteLine(" ErrorLog(\"加载配置%s出错\",Singleton<{0}>::Instance()->GetTableFile().c_str() );", meta.GetClassName()); 76 | sw.WriteLine(" return false;"); 77 | sw.WriteLine(" }"); 78 | } 79 | sw.WriteLine(" return true;"); 80 | sw.WriteLine("}"); 81 | sw.WriteLine(); 82 | 83 | foreach (var meta in metalist) 84 | { 85 | sw.WriteLine("bool {0}::Load()", meta.GetClassName()); 86 | sw.WriteLine("{"); 87 | sw.WriteLine(" ReadTableFile reader;"); 88 | sw.WriteLine(" reader.Initialize();"); 89 | sw.WriteLine(); 90 | sw.WriteLine(" if (!reader.Init(GetTableFile().c_str()))"); 91 | sw.WriteLine(" return false;"); 92 | sw.WriteLine(); 93 | sw.WriteLine(" DataReader dr;"); 94 | sw.WriteLine(" int iRows = reader.GetRowCount();"); 95 | sw.WriteLine(" int iCols = reader.GetColCount();"); 96 | sw.WriteLine(); 97 | sw.WriteLine(" for (int i = 1; i < iRows; ++i)"); 98 | sw.WriteLine(" {"); 99 | sw.WriteLine(" {0} item;", meta.GetItemName()); 100 | foreach (var field in meta.Fields) 101 | { 102 | switch (field.fieldType) 103 | { 104 | case TableFieldType.IntField: 105 | sw.WriteLine(" item.{0} = atoi(reader.GetValue(i, \"{0}\"));", field.fieldName); 106 | break; 107 | case TableFieldType.FloatField: 108 | sw.WriteLine(" item.{0} = atof(reader.GetValue(i, \"{0}\"));", field.fieldName); 109 | break; 110 | case TableFieldType.StringField: 111 | sw.WriteLine(" item.{0} = (reader.GetValue(i, \"{0}\"));", field.fieldName); 112 | break; 113 | case TableFieldType.IntList: 114 | sw.WriteLine(" item.{0} = dr.GetIntList(reader.GetValue(i, \"{0}\"));", field.fieldName); 115 | break; 116 | case TableFieldType.FloatList: 117 | sw.WriteLine(" item.{0} = dr.GetFloatList(reader.GetValue(i, \"{0}\"));", field.fieldName); 118 | break; 119 | case TableFieldType.StringList: 120 | sw.WriteLine(" item.{0} = dr.GetStringList(reader.GetValue(i, \"{0}\"));", field.fieldName); 121 | break; 122 | case TableFieldType.StructField: 123 | sw.WriteLine(" item.{0} = dr.GetObject<{1}>(reader.GetValue(i, \"{0}\"));", field.fieldName, field.GetCppTypeName()); 124 | break; 125 | case TableFieldType.StructList: 126 | sw.WriteLine(" item.{0} = dr.GetObjectList<{1}>(reader.GetValue(i, \"{0}\"));", field.fieldName, field.GetTypeNameOfStructList()); 127 | break; 128 | } 129 | } 130 | sw.WriteLine(" m_Items[item.id] = item;");//必须有一个id 131 | sw.WriteLine(" }"); 132 | sw.WriteLine(); 133 | sw.WriteLine(" return true;"); 134 | sw.WriteLine("}"); 135 | } 136 | /////////////////// 137 | 138 | } 139 | } 140 | } 141 | public static void MakeCppFileTbs(List metalist, string codepath) 142 | { 143 | string hfile = codepath + "tablestruct.h"; 144 | using (FileStream fs = new FileStream(hfile, FileMode.Create, FileAccess.Write)) 145 | { 146 | using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) 147 | { 148 | sw.WriteLine("//THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT!"); 149 | sw.WriteLine("//GENERATE TIME [{0}]", System.DateTime.Now.ToString()); 150 | sw.WriteLine("#pragma once"); 151 | sw.WriteLine("#include \"tabletool/readtablefield.h\""); 152 | sw.WriteLine("#include \"tabletool/myconfig.h\""); 153 | sw.WriteLine(); 154 | 155 | foreach (var meta in metalist) 156 | { 157 | sw.WriteLine(); 158 | sw.WriteLine("struct {0} : public ITableObject<{0}>", meta.TableName); 159 | sw.WriteLine("{"); 160 | foreach (var field in meta.Fields) 161 | { 162 | sw.WriteLine(" {0} {1};", field.GetCppTypeName(), field.fieldName); 163 | } 164 | sw.WriteLine(); 165 | sw.WriteLine(" virtual bool FromString(string s)"); 166 | sw.WriteLine(" {"); 167 | sw.WriteLine(" DataReader dr;"); 168 | sw.WriteLine(" vector vs = dr.GetStringList(s,',');"); 169 | sw.WriteLine(" if (vs.size() != {0})", meta.Fields.Count()); 170 | sw.WriteLine(" {"); 171 | sw.WriteLine(" ErrorLog(\"{0}字段配置错误\");", meta.TableName); 172 | sw.WriteLine(" return false;"); 173 | sw.WriteLine(" }"); 174 | for (int i = 0; i < meta.Fields.Count(); i++) 175 | { 176 | var field = meta.Fields[i]; 177 | switch (field.fieldType) 178 | { 179 | case TableFieldType.IntField: 180 | sw.WriteLine(" {0} = stoi(vs[{1}]);", field.fieldName, i); 181 | break; 182 | case TableFieldType.FloatField: 183 | sw.WriteLine(" {0} = stof(vs[{1}]);", field.fieldName, i); 184 | break; 185 | case TableFieldType.StringField: 186 | sw.WriteLine(" {0} = (vs[{1}]);", field.fieldName, i); 187 | break; 188 | default: 189 | Console.WriteLine("{0}.{1}字段类型错误,只能是int float string!", meta.TableName, field.fieldName); 190 | break; 191 | } 192 | } 193 | sw.WriteLine(" return true;"); 194 | sw.WriteLine(" }"); 195 | sw.WriteLine("};"); 196 | sw.WriteLine(); 197 | } 198 | //////////////////////// 199 | } 200 | } 201 | } 202 | 203 | public static void MakeCsharpFileTbs(List metalist, string codepath) 204 | { 205 | string hfile = codepath + "TableStruct.cs"; 206 | using (FileStream fs = new FileStream(hfile, FileMode.Create, FileAccess.Write)) 207 | { 208 | using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) 209 | { 210 | sw.WriteLine("//THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT!"); 211 | sw.WriteLine("//GENERATE TIME [{0}]", System.DateTime.Now.ToString()); 212 | sw.WriteLine(); 213 | sw.WriteLine("namespace tabtool"); 214 | sw.WriteLine("{"); 215 | foreach (var meta in metalist) 216 | { 217 | sw.WriteLine(" public class {0} : ITableObject", meta.TableName); 218 | sw.WriteLine(" {"); 219 | foreach (var field in meta.Fields) 220 | { 221 | sw.WriteLine(" {0} {1};", field.GetCsharpTypeName(), field.fieldName); 222 | } 223 | sw.WriteLine(" public bool FromString(string s)"); 224 | sw.WriteLine(" {"); 225 | sw.WriteLine(" DataReader dr = new DataReader();"); 226 | sw.WriteLine(); 227 | sw.WriteLine(" var vs = s.Split(',');"); 228 | sw.WriteLine(" if (vs.Length != 2)"); 229 | sw.WriteLine(" {"); 230 | sw.WriteLine(" return false;"); 231 | sw.WriteLine(" }"); 232 | sw.WriteLine(); 233 | for (int i = 0; i < meta.Fields.Count(); i++) 234 | { 235 | var field = meta.Fields[i]; 236 | switch (field.fieldType) 237 | { 238 | case TableFieldType.IntField: 239 | sw.WriteLine(" {0} = dr.GetInt(vs[{1}]);", field.fieldName, i); 240 | break; 241 | case TableFieldType.FloatField: 242 | sw.WriteLine(" {0} = dr.GetFloat(vs[{1}]);", field.fieldName, i); 243 | break; 244 | case TableFieldType.StringField: 245 | sw.WriteLine(" {0} = vs[{1}];", field.fieldName, i); 246 | break; 247 | default: 248 | Console.WriteLine("{0}.{1}字段类型错误,只能是int float string!", meta.TableName, field.fieldName); 249 | break; 250 | } 251 | } 252 | 253 | sw.WriteLine(" return true;"); 254 | sw.WriteLine(" }"); 255 | sw.WriteLine(" };"); 256 | sw.WriteLine(); 257 | } 258 | sw.WriteLine("}"); 259 | 260 | //////////////////////// 261 | } 262 | } 263 | 264 | } 265 | 266 | public static void MakeCsharpFile(List metalist, string codepath) 267 | { 268 | string csfile = codepath + "TableConfig.cs"; 269 | using (FileStream fs = new FileStream(csfile, FileMode.Create, FileAccess.Write)) 270 | { 271 | using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) 272 | { 273 | sw.WriteLine("//THIS FILE IS GENERATED BY tabtool, DO NOT EDIT IT!"); 274 | sw.WriteLine("//GENERATE TIME [{0}]", System.DateTime.Now.ToString()); 275 | sw.WriteLine(); 276 | sw.WriteLine("using System.Collections;"); 277 | sw.WriteLine("using System.Collections.Generic;"); 278 | sw.WriteLine("using System.Data;"); 279 | sw.WriteLine(); 280 | sw.WriteLine("namespace tabtool"); 281 | sw.WriteLine("{"); 282 | foreach (var meta in metalist) 283 | { 284 | sw.WriteLine(); 285 | sw.WriteLine(" public class {0}", meta.GetItemName()); 286 | sw.WriteLine(" {"); 287 | foreach (var field in meta.Fields) 288 | { 289 | sw.WriteLine(" public {0} {1};", field.GetCsharpTypeName(), field.fieldName); 290 | } 291 | sw.WriteLine(" }"); 292 | sw.WriteLine(); 293 | sw.WriteLine(" public class {0} : TableManager<{1}, {0}>", meta.GetClassName(),meta.GetItemName()); 294 | sw.WriteLine(" {"); 295 | sw.WriteLine(" public override bool Load()"); 296 | sw.WriteLine(" {"); 297 | sw.WriteLine(" TableReader tr = new TableReader();"); 298 | sw.WriteLine(" DataReader dr = new DataReader();"); 299 | sw.WriteLine(" DataTable dt = tr.ReadFile(MyConfig.WorkDir+\"{0}.txt\");",meta.TableName); 300 | sw.WriteLine(); 301 | sw.WriteLine(" foreach(DataRow row in dt.Rows)"); 302 | sw.WriteLine(" {"); 303 | sw.WriteLine(" var item = new {0}();", meta.GetItemName()); 304 | 305 | foreach (var field in meta.Fields) 306 | { 307 | switch (field.fieldType) 308 | { 309 | case TableFieldType.IntField: 310 | sw.WriteLine(" item.{0} = dr.GetInt(row[\"{0}\"].ToString());", field.fieldName); 311 | break; 312 | case TableFieldType.FloatField: 313 | sw.WriteLine(" item.{0} = dr.GetFloat(row[\"{0}\"].ToString());", field.fieldName); 314 | break; 315 | case TableFieldType.StringField: 316 | sw.WriteLine(" item.{0} = (row[\"{0}\"].ToString());", field.fieldName); 317 | break; 318 | case TableFieldType.IntList: 319 | sw.WriteLine(" item.{0} = dr.GetIntList(row[\"{0}\"].ToString());", field.fieldName); 320 | break; 321 | case TableFieldType.FloatList: 322 | sw.WriteLine(" item.{0} = dr.GetFloatList(row[\"{0}\"].ToString());", field.fieldName); 323 | break; 324 | case TableFieldType.StringList: 325 | sw.WriteLine(" item.{0} = dr.GetStringList(row[\"{0}\"].ToString());", field.fieldName); 326 | break; 327 | case TableFieldType.StructField: 328 | sw.WriteLine(" item.{0} = dr.GetObject<{1}>(row[\"{0}\"].ToString());", field.fieldName, field.GetCsharpTypeName()); 329 | break; 330 | case TableFieldType.StructList: 331 | sw.WriteLine(" item.{0} = dr.GetObjectList<{1}>(row[\"{0}\"].ToString());", field.fieldName, field.GetTypeNameOfStructList()); 332 | break; 333 | } 334 | } 335 | sw.WriteLine(" m_Items[item.id] = item;");//必须有一个id 336 | sw.WriteLine(" }"); 337 | sw.WriteLine(" return true;"); 338 | sw.WriteLine(" }"); 339 | sw.WriteLine(" }"); 340 | sw.WriteLine(); 341 | } 342 | 343 | sw.WriteLine(" public class TableConfig : SingletonTable"); 344 | sw.WriteLine(" {"); 345 | sw.WriteLine(" public bool LoadTableConfig()"); 346 | sw.WriteLine(" {"); 347 | 348 | foreach (var meta in metalist) 349 | { 350 | sw.WriteLine(" if (!{0}.Instance.Load()) return false;", meta.GetClassName()); 351 | } 352 | sw.WriteLine(" return true;"); 353 | sw.WriteLine(" }"); 354 | sw.WriteLine(" }"); 355 | 356 | sw.WriteLine("}"); 357 | /////////////////// 358 | 359 | } 360 | } 361 | 362 | } 363 | } 364 | } 365 | -------------------------------------------------------------------------------- /tabtool/ExcelHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Collections; 7 | using NPOI.SS.UserModel; 8 | using NPOI.HSSF.UserModel; 9 | using System.IO; 10 | using System.Data; 11 | using NPOI.XSSF.UserModel; 12 | using System.Xml; 13 | 14 | namespace tabtool 15 | { 16 | public class ExcelHelper 17 | { 18 | IWorkbook hssfworkbook; 19 | public DataTable ImportExcelFile(string filePath) 20 | { 21 | try 22 | { 23 | using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) 24 | { 25 | hssfworkbook = new XSSFWorkbook(file); 26 | } 27 | } 28 | catch (Exception e) 29 | { 30 | throw e; 31 | } 32 | 33 | ISheet sheet = hssfworkbook.GetSheetAt(0); 34 | System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); 35 | 36 | DataTable dt = new DataTable(); 37 | IRow row0 = sheet.GetRow(0); 38 | for (int j = row0.FirstCellNum; j < (row0.LastCellNum); j++) 39 | { 40 | dt.Columns.Add(row0.GetCell(j).ToString()); 41 | } 42 | 43 | while (rows.MoveNext()) 44 | { 45 | IRow row = (XSSFRow)rows.Current; 46 | DataRow dr = dt.NewRow(); 47 | 48 | for (int i = 0; i < row.LastCellNum; i++) 49 | { 50 | ICell cell = row.GetCell(i); 51 | if (cell == null) 52 | { 53 | dr[i] = null; 54 | } 55 | else 56 | { 57 | dr[i] = cell.ToString(); 58 | } 59 | } 60 | dt.Rows.Add(dr); 61 | } 62 | return dt; 63 | } 64 | 65 | public void ExportXmlFile(string filepath, DataTable dt, int firstrow) 66 | { 67 | using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write)) 68 | { 69 | XmlWriterSettings setting = new XmlWriterSettings(); 70 | setting.Indent = true; 71 | setting.Encoding = new UTF8Encoding(false); 72 | setting.NewLineChars = Environment.NewLine; 73 | 74 | using (XmlWriter xw = XmlWriter.Create(fs, setting)) 75 | { 76 | xw.WriteStartDocument(true); 77 | xw.WriteStartElement("datas"); 78 | for (int i = firstrow; i < dt.Rows.Count; i++) 79 | { 80 | xw.WriteStartElement("data"); 81 | for (int j = 0; j < dt.Columns.Count; j++) 82 | { 83 | xw.WriteAttributeString(dt.Columns[j].ToString(), dt.Rows[i].ItemArray[j].ToString()); 84 | } 85 | xw.WriteEndElement(); 86 | } 87 | xw.WriteEndElement(); 88 | xw.WriteEndDocument(); 89 | } 90 | } 91 | } 92 | 93 | public void ExportTxtFile(string filepath, DataTable dt, int firstrow) 94 | { 95 | using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write)) 96 | { 97 | StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); 98 | for (int i = firstrow; i < dt.Rows.Count; i++) 99 | { 100 | for (int j = 0; j < dt.Columns.Count; j++) 101 | { 102 | if (j == dt.Columns.Count - 1) 103 | { 104 | sw.WriteLine(dt.Rows[i].ItemArray[j].ToString()); 105 | } 106 | else 107 | { 108 | sw.Write(dt.Rows[i].ItemArray[j].ToString() + "\t"); 109 | } 110 | } 111 | } 112 | sw.Close(); 113 | } 114 | } 115 | 116 | public bool IsExportFile(string key, DataTable dt) 117 | { 118 | return IsExportField(key, dt, 0); 119 | } 120 | 121 | public bool IsExportField(string key, DataTable dt, int col) 122 | { 123 | if (string.Compare(dt.Rows[3].ItemArray[col].ToString(), "all", true) == 0) 124 | { 125 | return true; 126 | } 127 | if (string.Compare(dt.Rows[3].ItemArray[col].ToString(), key, true) == 0) 128 | { 129 | return true; 130 | } 131 | return false; 132 | } 133 | 134 | internal TableMeta ParseTableMeta(string filename, DataTable dt, string cmp) 135 | { 136 | TableMeta meta = new TableMeta(); 137 | meta.TableName = filename; 138 | //第0行注释 第一行name 第二行type 139 | for (int i = 0; i < dt.Columns.Count; i++) 140 | { 141 | if (!IsExportField(cmp, dt, i)) { continue; } 142 | TableField field = new TableField(); 143 | field.fieldName = dt.Rows[1].ItemArray[i].ToString(); 144 | field.typeName = dt.Rows[2].ItemArray[i].ToString(); 145 | if (field.typeName == "int") { field.fieldType = TableFieldType.IntField; } 146 | else if (field.typeName == "float") { field.fieldType = TableFieldType.FloatField; } 147 | else if (field.typeName == "string") { field.fieldType = TableFieldType.StringField; } 148 | else if (field.typeName == "int+") { field.fieldType = TableFieldType.IntList; } 149 | else if (field.typeName == "float+") { field.fieldType = TableFieldType.FloatList; } 150 | else if (field.typeName == "string+") { field.fieldType = TableFieldType.StringList; } 151 | else if (field.typeName[field.typeName.Length - 1] == '+') { field.fieldType = TableFieldType.StructList; } 152 | else { field.fieldType = TableFieldType.StructField; } 153 | meta.Fields.Add(field); 154 | } 155 | return meta; 156 | } 157 | 158 | public void ExportTxtFileEx(string filepath, DataTable dt, string key, int[] ignorerows) 159 | { 160 | using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write)) 161 | { 162 | StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); 163 | for (int i = 0; i < dt.Rows.Count; i++) 164 | { 165 | if (ignorerows.Contains(i)) continue; 166 | for (int j = 0; j < dt.Columns.Count; j++) 167 | { 168 | if (!IsExportField(key,dt,j)) 169 | { 170 | if (j == dt.Columns.Count - 1) 171 | { 172 | sw.Write("\n"); 173 | } 174 | continue; 175 | } 176 | if (j == dt.Columns.Count - 1) 177 | { 178 | sw.WriteLine("\t" + dt.Rows[i].ItemArray[j].ToString()); 179 | } 180 | else if (j == 0) 181 | { 182 | sw.Write(dt.Rows[i].ItemArray[j].ToString()); 183 | } 184 | else 185 | { 186 | sw.Write("\t" + dt.Rows[i].ItemArray[j].ToString()); 187 | } 188 | } 189 | } 190 | sw.Close(); 191 | } 192 | } 193 | 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /tabtool/NPOI/ICSharpCode.SharpZipLib.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/tabtool/NPOI/ICSharpCode.SharpZipLib.dll -------------------------------------------------------------------------------- /tabtool/NPOI/NPOI.OOXML.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/tabtool/NPOI/NPOI.OOXML.dll -------------------------------------------------------------------------------- /tabtool/NPOI/NPOI.OpenXml4Net.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/tabtool/NPOI/NPOI.OpenXml4Net.dll -------------------------------------------------------------------------------- /tabtool/NPOI/NPOI.OpenXmlFormats.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/tabtool/NPOI/NPOI.OpenXmlFormats.dll -------------------------------------------------------------------------------- /tabtool/NPOI/NPOI.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/tabtool/NPOI/NPOI.dll -------------------------------------------------------------------------------- /tabtool/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Data; 7 | using System.Xml; 8 | using System.IO; 9 | 10 | namespace tabtool 11 | { 12 | class Program 13 | { 14 | static void Main(string[] args) 15 | { 16 | string clientOutDir, serverOutDir, cppOutDir, csOutDir, excelDir, metafile; 17 | CmdlineHelper cmder = new CmdlineHelper(args); 18 | if (cmder.Has("--out_client")) { clientOutDir = cmder.Get("--out_client"); } else { return; } 19 | if (cmder.Has("--out_server")) { serverOutDir = cmder.Get("--out_server"); } else { return; } 20 | if (cmder.Has("--in_excel")) { excelDir = cmder.Get("--in_excel"); } else { return; } 21 | if (cmder.Has("--in_tbs")) { metafile = cmder.Get("--in_tbs"); } else { return; } 22 | 23 | //创建导出目录 24 | if (!Directory.Exists(clientOutDir)) Directory.CreateDirectory(clientOutDir); 25 | if (!Directory.Exists(serverOutDir)) Directory.CreateDirectory(serverOutDir); 26 | 27 | //先读取tablemata文件 28 | TableStruct tbs = new TableStruct(); 29 | if(!tbs.ImportTableStruct(metafile)) 30 | { 31 | Console.WriteLine("解析tbs文件错误!"); 32 | return; 33 | } 34 | Console.WriteLine("解析tbs文件成功"); 35 | 36 | List clientTableMetaList = new List(); 37 | List serverTableMetaList = new List(); 38 | 39 | //导出文件 40 | ExcelHelper helper = new ExcelHelper(); 41 | string[] files = Directory.GetFiles(excelDir, "*.xlsx", SearchOption.TopDirectoryOnly); 42 | foreach (string filepath in files) 43 | { 44 | string xmlfile = clientOutDir + Path.GetFileNameWithoutExtension(filepath) + ".txt"; 45 | string txtfile = serverOutDir + Path.GetFileNameWithoutExtension(filepath) + ".txt"; 46 | try 47 | { 48 | DataTable dt = helper.ImportExcelFile(filepath); 49 | 50 | if (helper.IsExportFile("client", dt)) 51 | { 52 | helper.ExportTxtFileEx(xmlfile, dt, "client", new int[] { 0, 2, 3 }); 53 | TableMeta meta = helper.ParseTableMeta(Path.GetFileNameWithoutExtension(filepath), dt, "client"); 54 | clientTableMetaList.Add(meta); 55 | } 56 | if (helper.IsExportFile("server", dt)) 57 | { 58 | helper.ExportTxtFileEx(txtfile, dt, "server", new int[] { 0, 2, 3 }); 59 | TableMeta meta = helper.ParseTableMeta(Path.GetFileNameWithoutExtension(filepath), dt, "server"); 60 | serverTableMetaList.Add(meta); 61 | } 62 | 63 | } 64 | catch (Exception e) 65 | { 66 | Console.WriteLine(filepath + " 文件出错!"); 67 | Console.WriteLine(e.ToString()); 68 | } 69 | } 70 | Console.WriteLine("导出配置文件成功"); 71 | 72 | //生成代码 73 | if (cmder.Has("--out_cpp")) 74 | { 75 | cppOutDir = cmder.Get("--out_cpp"); 76 | if (!Directory.Exists(cppOutDir)) 77 | Directory.CreateDirectory(cppOutDir); 78 | 79 | CodeGen.MakeCppFileTbs(tbs.GetMetaList(), cppOutDir); 80 | CodeGen.MakeCppFile(serverTableMetaList, cppOutDir); 81 | Console.WriteLine("生成.cpp代码文件成功"); 82 | 83 | } 84 | 85 | if (cmder.Has("--out_cs")) 86 | { 87 | csOutDir = cmder.Get("--out_cs"); 88 | if (!Directory.Exists(csOutDir)) 89 | Directory.CreateDirectory(csOutDir); 90 | 91 | CodeGen.MakeCsharpFileTbs(tbs.GetMetaList(), csOutDir); 92 | CodeGen.MakeCsharpFile(clientTableMetaList, csOutDir); 93 | Console.WriteLine("生成.cs代码文件成功"); 94 | } 95 | 96 | Console.WriteLine("按任意键退出..."); 97 | Console.ReadKey(false); 98 | 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /tabtool/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("tabtool")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("tabtool")] 13 | [assembly: AssemblyCopyright("Copyright © 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e1ddf9d0-972a-4e1b-a700-9f6d72d838f7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /tabtool/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace tabtool.Properties 12 | { 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// Returns the cached ResourceManager instance used by this class. 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("tabtool.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// Overrides the current thread's CurrentUICulture property for all 56 | /// resource lookups using this strongly typed resource class. 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /tabtool/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 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 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /tabtool/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace tabtool.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tabtool/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /tabtool/TableMeta.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace tabtool 8 | { 9 | enum TableFieldType 10 | { 11 | IntField, 12 | FloatField, 13 | StringField, 14 | StructField, 15 | IntList, 16 | FloatList, 17 | StringList, 18 | StructList, 19 | } 20 | 21 | class TableField 22 | { 23 | public TableFieldType fieldType; 24 | public string fieldName; 25 | public string typeName; 26 | 27 | public string GetCppTypeName() 28 | { 29 | string[] ts = { "int", "float", "string", "xxx", "vector", "vector", "vector", "xxx" }; 30 | if (fieldType == TableFieldType.StructField) 31 | { 32 | return typeName; 33 | } 34 | if (fieldType == TableFieldType.StructList) 35 | { 36 | return string.Format("vector<{0}>", typeName.Substring(0, typeName.Length - 1)); 37 | } 38 | return ts[(int)fieldType]; 39 | } 40 | 41 | public string GetTypeNameOfStructList() 42 | { 43 | return typeName.Substring(0, typeName.Length - 1); 44 | } 45 | 46 | 47 | public string GetCsharpTypeName() 48 | { 49 | string[] ts = { "int", "float", "string", "xxx", "List", "List", "List", "xxx" }; 50 | if (fieldType == TableFieldType.StructField) 51 | { 52 | return typeName; 53 | } 54 | if (fieldType == TableFieldType.StructList) 55 | { 56 | return string.Format("List<{0}>", typeName.Substring(0, typeName.Length - 1)); 57 | } 58 | return ts[(int)fieldType]; 59 | } 60 | 61 | } 62 | 63 | class TableMeta 64 | { 65 | public string TableName; 66 | public List Fields = new List(); 67 | public string GetClassName() 68 | { 69 | return "cfg" + TableName + "Table"; 70 | } 71 | 72 | public string GetItemName() 73 | { 74 | return "tbs" + TableName + "Item"; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /tabtool/TableStruct.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.IO; 7 | using System.Text.RegularExpressions; 8 | 9 | namespace tabtool 10 | { 11 | class TableStruct 12 | { 13 | enum ParseState 14 | { 15 | EndStruct, 16 | BeginStruct, 17 | } 18 | 19 | ParseState m_parseState = ParseState.EndStruct; 20 | TableMeta m_tableMeta = null; 21 | List m_metaList = new List(); 22 | 23 | public List GetMetaList() 24 | { 25 | return m_metaList; 26 | } 27 | 28 | public TableMeta GetTableMetaByName(string name) 29 | { 30 | foreach (var meta in m_metaList) 31 | { 32 | if (meta.TableName == name) 33 | { 34 | return meta; 35 | } 36 | } 37 | return null; 38 | } 39 | 40 | public bool ImportTableStruct(string filepath) 41 | { 42 | m_metaList.Clear(); 43 | string[] lines = File.ReadAllLines(filepath); 44 | for (int i = 0; i < lines.Count(); i++) 45 | { 46 | if (Regex.IsMatch(lines[i], @"^\s*//.*$")) { continue; }//注释 47 | if (Regex.IsMatch(lines[i], @"^\s*$")) { continue; }//空行 48 | if (Regex.IsMatch(lines[i], @"^\s*\w+\s+{\s*$"))//结构体开始 49 | { 50 | if (m_parseState != ParseState.EndStruct) 51 | { 52 | Console.WriteLine("tbs文件错误:第{0}行", i); 53 | return false; 54 | } 55 | m_parseState = ParseState.BeginStruct; 56 | Match match = Regex.Match(lines[i], @"^\s*(\w+)\s+{\s*$"); 57 | m_tableMeta = new TableMeta(); 58 | m_tableMeta.TableName = match.Groups[1].Value; 59 | continue; 60 | } 61 | if (Regex.IsMatch(lines[i], @"^\s*}\s*$"))//结构体结束 62 | { 63 | if(m_parseState != ParseState.BeginStruct) 64 | { 65 | Console.WriteLine("tbs文件错误:第{0}行", i); 66 | return false; 67 | } 68 | m_parseState = ParseState.EndStruct; 69 | m_metaList.Add(m_tableMeta); 70 | continue; 71 | } 72 | if (m_parseState == ParseState.BeginStruct) 73 | { 74 | Match m = Regex.Match(lines[i], @"\s*(\w+)\s+(\w+)\s*$");//var type 75 | if (m.Success == false) 76 | { 77 | Console.WriteLine("tbs文件错误:第{0}行", i); 78 | return false; 79 | } 80 | 81 | TableField field = new TableField(); 82 | field.fieldName = m.Groups[1].Value; 83 | field.typeName = m.Groups[2].Value; 84 | switch (field.typeName) 85 | { 86 | case "int": { field.fieldType = TableFieldType.IntField; break; } 87 | case "float": { field.fieldType = TableFieldType.FloatField; break; } 88 | case "string": { field.fieldType = TableFieldType.StringField; break; } 89 | case "int+": { field.fieldType = TableFieldType.IntList; break; } 90 | case "float+": { field.fieldType = TableFieldType.FloatList; break; } 91 | case "string+": { field.fieldType = TableFieldType.StringList; break; } 92 | default: 93 | Console.WriteLine("tbs文件错误:第{0}行", i); 94 | return false; 95 | } 96 | m_tableMeta.Fields.Add(field); 97 | continue; 98 | } 99 | 100 | Console.WriteLine("tbs文件错误:第{0}行", i); 101 | return false; 102 | } 103 | return true; 104 | } 105 | 106 | 107 | //////////////// 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /tabtool/tabtool.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {E1DDF9D0-972A-4E1B-A700-9F6D72D838F7} 8 | Exe 9 | Properties 10 | tabtool 11 | tabtool 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | NPOI\ICSharpCode.SharpZipLib.dll 41 | 42 | 43 | NPOI\NPOI.dll 44 | 45 | 46 | NPOI\NPOI.OOXML.dll 47 | 48 | 49 | NPOI\NPOI.OpenXml4Net.dll 50 | 51 | 52 | NPOI\NPOI.OpenXmlFormats.dll 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | ResXFileCodeGenerator 76 | Resources.Designer.cs 77 | Designer 78 | 79 | 80 | True 81 | Resources.resx 82 | 83 | 84 | SettingsSingleFileGenerator 85 | Settings.Designer.cs 86 | 87 | 88 | True 89 | Settings.settings 90 | True 91 | 92 | 93 | 94 | 95 | 96 | 97 | 104 | -------------------------------------------------------------------------------- /test/Achive.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jwk000/tabtool/58474b13f1a2eae79e523317381580c407866676/test/Achive.xlsx -------------------------------------------------------------------------------- /test/meta.tbs: -------------------------------------------------------------------------------- 1 | //支持注释 2 | //表示id和数量 3 | tbsIdCount { 4 | id int 5 | count int 6 | } 7 | 8 | tbsKeyValue { 9 | key int 10 | value int 11 | } 12 | 13 | tbsTest { 14 | a int 15 | b string 16 | c float 17 | } -------------------------------------------------------------------------------- /test/一键导表.bat: -------------------------------------------------------------------------------- 1 | "../tabtool/bin/Debug/tabtool.exe" --out_client ../csharptest/config/ --out_server ../cpptest/config/ --out_cpp ../cpptest/ --out_cs ../csharptest/ --in_excel ./ --in_tbs ./meta.tbs 2 | --------------------------------------------------------------------------------