Skip to content

Commit 7a1bf40

Browse files
committed
添加配置表读取方式
1 parent 7516926 commit 7a1bf40

File tree

11 files changed

+227
-33
lines changed

11 files changed

+227
-33
lines changed

server/src/main/java/info/xiaomo/server/config/TestConfig.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package info.xiaomo.server.config.beans;
2+
3+
import info.xiaomo.gameCore.config.IConfig;
4+
import info.xiaomo.gameCore.config.annotation.Column;
5+
import info.xiaomo.gameCore.config.annotation.Config;
6+
import info.xiaomo.gameCore.config.annotation.Table;
7+
import info.xiaomo.server.config.converters.IntegerArrayConverter;
8+
import info.xiaomo.server.config.converters.IntegerMapConverter;
9+
import info.xiaomo.server.config.converters.Matrix3IntConverter;
10+
import lombok.Data;
11+
import lombok.ToString;
12+
13+
import java.util.Map;
14+
15+
@ToString
16+
@Data
17+
@Config
18+
@Table(name = "cfg_item", primaryKey = {"id", "secondId"})
19+
public class ItemConfig implements IConfig {
20+
@Column(notNull = true)
21+
private int id;
22+
23+
@Column(name = "id2")
24+
private String secondId;
25+
26+
// 多个转换器共同使用 先转成int数组 然后将int数组转为map
27+
@Column({IntegerArrayConverter.class, IntegerMapConverter.class})
28+
private Map<Integer, Integer> map;
29+
30+
@Column(name = "ints")
31+
private int[] arrays;
32+
33+
@Column(Matrix3IntConverter.class)
34+
private int[][][] intss;
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 创建日期: 2017年08月19日 9:57
3+
* 创建作者: 杨 强 <[email protected]>
4+
*/
5+
package info.xiaomo.server.config.converters;
6+
7+
8+
import info.xiaomo.gameCore.config.IConverter;
9+
10+
/**
11+
* @author YangQiang
12+
*/
13+
public class IntegerArrayConverter implements IConverter<String, int[]> {
14+
@Override
15+
public int[] convert(String s) {
16+
String[] strs = s.split("#");
17+
int[] ret = new int[strs.length];
18+
for (int i = 0; i < ret.length; i++) {
19+
ret[i] = Integer.parseInt(strs[i]);
20+
}
21+
return ret;
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 创建日期: 2017年08月19日 10:08
3+
* 创建作者: 杨 强 <[email protected]>
4+
*/
5+
package info.xiaomo.server.config.converters;
6+
7+
8+
import info.xiaomo.gameCore.config.IConverter;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
/**
14+
* @author YangQiang
15+
*/
16+
public class IntegerMapConverter implements IConverter<int[], Map<Integer, Integer>> {
17+
@Override
18+
public Map<Integer, Integer> convert(int[] ints) {
19+
Map<Integer, Integer> map = new HashMap<>();
20+
if (ints != null) {
21+
for (int i = 1; i < ints.length; i += 2) {
22+
map.put(ints[i - 1], ints[i]);
23+
}
24+
}
25+
return map;
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 创建日期: 2017年08月19日 10:01
3+
* 创建作者: 杨 强 <[email protected]>
4+
*/
5+
package info.xiaomo.server.config.converters;
6+
7+
8+
import info.xiaomo.gameCore.config.IConverter;
9+
10+
import java.util.Map;
11+
12+
/**
13+
* @author YangQiang
14+
*/
15+
public class MapConverter implements IConverter<String, Map<Integer, Integer>> {
16+
@Override
17+
public Map<Integer, Integer> convert(String s) {
18+
return new IntegerArrayConverter().andThen(new IntegerMapConverter()).convert(s);
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 创建日期: 2017年08月24日 10:56
3+
* 创建作者: 杨 强 <[email protected]>
4+
*/
5+
package info.xiaomo.server.config.converters;
6+
7+
import info.xiaomo.gameCore.config.IConverter;
8+
import org.apache.commons.beanutils.converters.ArrayConverter;
9+
import org.apache.commons.beanutils.converters.IntegerConverter;
10+
11+
/**
12+
* @author YangQiang
13+
*/
14+
public class Matrix3IntConverter implements IConverter<String, int[][][]> {
15+
@Override
16+
public int[][][] convert(String str) {
17+
if (str == null || str.isEmpty()) {
18+
return new int[0][][];
19+
}
20+
// 格式 1,2;3,4|5,6 => [[[1, 2], [3, 4]], [[5,6]]]
21+
IntegerConverter integerConverter = new IntegerConverter(); // 基本类型
22+
ArrayConverter arrayConverter1 = new ArrayConverter(int[].class, integerConverter); // 一维数组 默认使用逗号分割
23+
24+
ArrayConverter arrayConverter2 = new ArrayConverter(int[][].class, arrayConverter1); // 二维数组
25+
arrayConverter2.setDelimiter(';'); // 使用分号分割
26+
arrayConverter2.setAllowedChars(new char[]{','});
27+
28+
29+
ArrayConverter arrayConverter3 = new ArrayConverter(int[][].class, arrayConverter2); // 二维数组
30+
arrayConverter3.setDelimiter('|'); // 使用竖线分割
31+
arrayConverter3.setAllowedChars(new char[]{';', ','});
32+
33+
return arrayConverter3.convert(int[][][].class, str);
34+
}
35+
}

server/src/main/java/info/xiaomo/server/dataConfig/ConfigDataManager.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,24 @@ public void init() throws Exception {
5050
manager.init();
5151
}
5252

53+
public void init(String configPath) throws Exception {
54+
manager.setExcelFileDir(configPath);
55+
manager.setConfigPackageName("info.xiaomo.server.config.beans");
56+
init();
57+
}
58+
59+
60+
public void setResourceRoot(String resourceRoot) {
61+
manager.setExcelFileDir(resourceRoot);
62+
}
63+
64+
public void setConfigPackagePath(String packagePath){
65+
manager.setConfigPackageName(packagePath);
66+
}
67+
68+
public void setSubfix(String subfix) {
69+
manager.setExcelFileSuffix(subfix);
70+
}
71+
5372

5473
}

server/src/main/java/info/xiaomo/server/server/GameServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import info.xiaomo.gameCore.protocol.NetworkService;
44
import info.xiaomo.gameCore.protocol.NetworkServiceBuilder;
55
import info.xiaomo.server.constant.GameConst;
6+
import info.xiaomo.server.dataConfig.ConfigDataManager;
67
import info.xiaomo.server.db.DataCenter;
78
import info.xiaomo.server.event.EventRegister;
89
import info.xiaomo.server.processor.LogicProcessor;
@@ -63,7 +64,7 @@ public GameServer(ServerOption option) throws Exception {
6364
DataCenter.init(option);
6465

6566
//初始化配置文件
66-
//ConfigDataManager.getInstance().init(option.getConfigDataPath());
67+
ConfigDataManager.getInstance().init(option.getConfigDataPath());
6768

6869
// 注册事件
6970
EventRegister.registerPreparedListeners();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package info.xiaomo.server.test;
2+
3+
import info.xiaomo.gameCore.config.IConfig;
4+
import info.xiaomo.gameCore.config.annotation.Column;
5+
import info.xiaomo.gameCore.config.annotation.Config;
6+
import info.xiaomo.gameCore.config.annotation.Table;
7+
import info.xiaomo.server.config.converters.IntegerArrayConverter;
8+
import info.xiaomo.server.config.converters.IntegerMapConverter;
9+
import info.xiaomo.server.config.converters.Matrix3IntConverter;
10+
import lombok.Data;
11+
import lombok.ToString;
12+
13+
import java.util.Map;
14+
15+
@ToString
16+
@Data
17+
@Config
18+
@Table(name = "cfg_item", primaryKey = {"id", "secondId"})
19+
public class ItemConfig implements IConfig {
20+
@Column(notNull = true)
21+
private int id;
22+
23+
@Column(name = "id2")
24+
private String secondId;
25+
26+
// 多个转换器共同使用 先转成int数组 然后将int数组转为map
27+
@Column({IntegerArrayConverter.class, IntegerMapConverter.class})
28+
private Map<Integer, Integer> map;
29+
30+
@Column(name = "ints")
31+
private int[] arrays;
32+
33+
@Column(Matrix3IntConverter.class)
34+
private int[][][] intss;
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package info.xiaomo.server.test;
2+
3+
import info.xiaomo.server.config.beans.ItemConfig;
4+
import info.xiaomo.server.dataConfig.ConfigDataManager;
5+
6+
import java.util.List;
7+
8+
/**
9+
* 把今天最好的表现当作明天最新的起点..~
10+
* いま 最高の表現 として 明日最新の始発..~
11+
* Today the best performance as tomorrow newest starter!
12+
* Created by IntelliJ IDEA.
13+
* <p>
14+
* author: xiaomo
15+
* github: https://github.com/xiaomoinfo
16+
17+
* QQ : 83387856
18+
* Date : 2017/8/24 13:09
19+
* desc :
20+
* Copyright(©) 2017 by xiaomo.
21+
*/
22+
public class TestExcelConfig {
23+
24+
public static void main(String[] args) throws Exception {
25+
ConfigDataManager.getInstance().setConfigPackagePath("info.xiaomo.server.config.beans");
26+
ConfigDataManager.getInstance().setResourceRoot(TestExcelConfig.class.getClassLoader().getResource("").getPath());
27+
ConfigDataManager.getInstance().init();
28+
ConfigDataManager.getInstance().getConfigs(ItemConfig.class).forEach(System.out::println);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)