Skip to content

Commit 106f6dc

Browse files
Merge pull request #8 from softwaremagico/npcCache
Npc cache
2 parents 0d49c61 + c26f392 commit 106f6dc

File tree

19 files changed

+699
-77
lines changed

19 files changed

+699
-77
lines changed

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
9393
<checkstyle.version>2.17</checkstyle.version>
9494
<spotbugs.version>4.1.3</spotbugs.version>
95+
<exec-maven-plugin.version>3.0.0</exec-maven-plugin.version>
96+
97+
<gson.cache.folder>${main.basedir}/modules/Fading Suns Revised Edition/json</gson.cache.folder>
9598
</properties>
9699

97100
<build>

think-machine-random/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
<properties>
1515
<main.basedir>${basedir}/..</main.basedir>
16+
<gson.cache.tmp.folder>${main.basedir}/think-machine-random/target/classes/Fading Suns Revised Edition/json
17+
</gson.cache.tmp.folder>
1618
</properties>
1719

1820
<build>
@@ -29,7 +31,48 @@
2931
</suiteXmlFiles>
3032
<useSystemClassLoader>false</useSystemClassLoader>
3133
</configuration>
34+
</plugin><!-- Generates new json cached -->
35+
<plugin>
36+
<groupId>org.codehaus.mojo</groupId>
37+
<artifactId>exec-maven-plugin</artifactId>
38+
<version>${exec-maven-plugin.version}</version>
39+
<executions>
40+
<execution>
41+
<phase>compile</phase>
42+
<goals>
43+
<goal>java</goal>
44+
</goals>
45+
</execution>
46+
</executions>
47+
<configuration>
48+
<mainClass>com.softwaremagico.tm.cache.FactoryCacheGenerator</mainClass>
49+
<skip>false</skip>
50+
<quietLogs>true</quietLogs>
51+
</configuration>
3252
</plugin>
53+
<!-- Copies and replace json cached -->
54+
<plugin>
55+
<artifactId>maven-antrun-plugin</artifactId>
56+
<version>${maven-antrun-plugin.version}</version>
57+
<executions>
58+
<execution>
59+
<phase>compile</phase>
60+
<configuration>
61+
<failOnError>false</failOnError>
62+
<target>
63+
<!-- copy json files -->
64+
<copy todir="${gson.cache.folder}" overwrite="true">
65+
<fileset dir="${gson.cache.tmp.folder}" includes="**" />
66+
</copy>
67+
</target>
68+
</configuration>
69+
<goals>
70+
<goal>run</goal>
71+
</goals>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
3376
</plugins>
3477
</build>
3578

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.softwaremagico.tm.cache;
2+
3+
/*-
4+
* #%L
5+
* Think Machine (Rules)
6+
* %%
7+
* Copyright (C) 2017 - 2021 Softwaremagico
8+
* %%
9+
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
10+
* <[email protected]> Valencia (Spain).
11+
*
12+
* This program is free software; you can redistribute it and/or modify it under
13+
* the terms of the GNU General Public License as published by the Free Software
14+
* Foundation; either version 2 of the License, or (at your option) any later
15+
* version.
16+
*
17+
* This program is distributed in the hope that it will be useful, but WITHOUT
18+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20+
* details.
21+
*
22+
* You should have received a copy of the GNU General Public License along with
23+
* this program; If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
24+
* #L%
25+
*/
26+
27+
import ch.qos.logback.classic.Level;
28+
import ch.qos.logback.classic.Logger;
29+
import com.softwaremagico.tm.InvalidXmlElementException;
30+
import com.softwaremagico.tm.file.modules.ModuleManager;
31+
import com.softwaremagico.tm.log.ConfigurationLog;
32+
import com.softwaremagico.tm.log.MachineModulesLog;
33+
import com.softwaremagico.tm.log.MachineXmlReaderLog;
34+
import com.softwaremagico.tm.random.predefined.characters.NpcFactory;
35+
import org.reflections.Reflections;
36+
import org.slf4j.LoggerFactory;
37+
38+
public class FactoryCacheGenerator {
39+
40+
public static void main(String[] args) throws InvalidXmlElementException {
41+
disableLogs();
42+
final NpcFactoryCacheLoader npcFactoryCacheLoader = new NpcFactoryCacheLoader();
43+
for (final String moduleName : ModuleManager.getAvailableModules()) {
44+
npcFactoryCacheLoader.save(NpcFactory.class, moduleName, NpcFactory.getInstance().getTranslatorFile());
45+
}
46+
}
47+
48+
private static void disableLogs() {
49+
Logger logger = (Logger) LoggerFactory.getLogger(MachineXmlReaderLog.class);
50+
logger.setLevel(Level.OFF);
51+
logger = (Logger) LoggerFactory.getLogger(ConfigurationLog.class);
52+
logger.setLevel(Level.OFF);
53+
logger = (Logger) LoggerFactory.getLogger(MachineModulesLog.class);
54+
logger.setLevel(Level.OFF);
55+
logger = (Logger) LoggerFactory.getLogger(Reflections.class);
56+
logger.setLevel(Level.OFF);
57+
}
58+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.softwaremagico.tm.cache;
2+
3+
/*-
4+
* #%L
5+
* Think Machine (Random Generator)
6+
* %%
7+
* Copyright (C) 2017 - 2021 Softwaremagico
8+
* %%
9+
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
10+
* <[email protected]> Valencia (Spain).
11+
*
12+
* This program is free software; you can redistribute it and/or modify it under
13+
* the terms of the GNU General Public License as published by the Free Software
14+
* Foundation; either version 2 of the License, or (at your option) any later
15+
* version.
16+
*
17+
* This program is distributed in the hope that it will be useful, but WITHOUT
18+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20+
* details.
21+
*
22+
* You should have received a copy of the GNU General Public License along with
23+
* this program; If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
24+
* #L%
25+
*/
26+
27+
import com.softwaremagico.tm.InvalidXmlElementException;
28+
import com.softwaremagico.tm.log.MachineLog;
29+
import com.softwaremagico.tm.random.party.RandomPartyFactory;
30+
import com.softwaremagico.tm.random.predefined.characters.NpcFactory;
31+
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
import java.util.concurrent.CompletableFuture;
35+
import java.util.concurrent.atomic.AtomicInteger;
36+
37+
public class ModuleLoaderEnforcer {
38+
39+
public static int loadAllFactories(String language, String moduleName) {
40+
final long startTime = System.nanoTime();
41+
final AtomicInteger loadedElements = new AtomicInteger(0);
42+
43+
loadedElements.addAndGet(com.softwaremagico.tm.file.modules.ModuleLoaderEnforcer.loadAllFactories(language, moduleName));
44+
45+
List<CompletableFuture<Void>> futures = new ArrayList<>();
46+
47+
futures.add(CompletableFuture.runAsync(() -> {
48+
try {
49+
loadedElements.addAndGet(RandomPartyFactory.getInstance().getElements(language, moduleName).size());
50+
} catch (InvalidXmlElementException e) {
51+
MachineLog.errorMessage(com.softwaremagico.tm.file.modules.ModuleLoaderEnforcer.class.getName(), e);
52+
}
53+
}));
54+
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
55+
futures = new ArrayList<>();
56+
57+
//Second level
58+
futures.add(CompletableFuture.runAsync(() -> {
59+
try {
60+
loadedElements.addAndGet(NpcFactory.getInstance().getElements(language, moduleName).size());
61+
} catch (InvalidXmlElementException e) {
62+
MachineLog.errorMessage(com.softwaremagico.tm.file.modules.ModuleLoaderEnforcer.class.getName(), e);
63+
}
64+
}));
65+
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
66+
67+
68+
final long duration = (System.nanoTime() - startTime);
69+
MachineLog.info(com.softwaremagico.tm.file.modules.ModuleLoaderEnforcer.class.getName(),
70+
"All factories loaded! Total {} elements loaded in {} milliseconds.",
71+
loadedElements.get(), duration / 1000000);
72+
return loadedElements.get();
73+
}
74+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.softwaremagico.tm.cache;
2+
3+
/*-
4+
* #%L
5+
* Think Machine (Rules)
6+
* %%
7+
* Copyright (C) 2017 - 2021 Softwaremagico
8+
* %%
9+
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
10+
* <[email protected]> Valencia (Spain).
11+
*
12+
* This program is free software; you can redistribute it and/or modify it under
13+
* the terms of the GNU General Public License as published by the Free Software
14+
* Foundation; either version 2 of the License, or (at your option) any later
15+
* version.
16+
*
17+
* This program is distributed in the hope that it will be useful, but WITHOUT
18+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20+
* details.
21+
*
22+
* You should have received a copy of the GNU General Public License along with
23+
* this program; If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
24+
* #L%
25+
*/
26+
27+
import com.google.gson.GsonBuilder;
28+
import com.softwaremagico.tm.InvalidXmlElementException;
29+
import com.softwaremagico.tm.character.benefices.AvailableBenefice;
30+
import com.softwaremagico.tm.character.benefices.BeneficeDefinition;
31+
import com.softwaremagico.tm.character.blessings.Blessing;
32+
import com.softwaremagico.tm.character.characteristics.Characteristic;
33+
import com.softwaremagico.tm.character.characteristics.CharacteristicDefinition;
34+
import com.softwaremagico.tm.character.equipment.DamageType;
35+
import com.softwaremagico.tm.character.equipment.armours.Armour;
36+
import com.softwaremagico.tm.character.equipment.shields.Shield;
37+
import com.softwaremagico.tm.character.equipment.weapons.Accessory;
38+
import com.softwaremagico.tm.character.equipment.weapons.Ammunition;
39+
import com.softwaremagico.tm.character.equipment.weapons.Weapon;
40+
import com.softwaremagico.tm.character.factions.Faction;
41+
import com.softwaremagico.tm.character.occultism.OccultismPath;
42+
import com.softwaremagico.tm.character.races.Race;
43+
import com.softwaremagico.tm.character.skills.AvailableSkill;
44+
import com.softwaremagico.tm.json.*;
45+
import com.softwaremagico.tm.json.factories.FactoryElements;
46+
import com.softwaremagico.tm.json.factories.cache.FactoryCacheLoader;
47+
import com.softwaremagico.tm.json.factories.cache.InvalidCacheFile;
48+
import com.softwaremagico.tm.random.predefined.characters.Npc;
49+
import com.softwaremagico.tm.random.predefined.characters.NpcFactory;
50+
import com.softwaremagico.tm.random.selectors.IRandomPreference;
51+
52+
import java.util.List;
53+
54+
public class NpcFactoryCacheLoader extends FactoryCacheLoader<Npc> {
55+
56+
@Override
57+
public List<Npc> load(String language, String moduleName) {
58+
try {
59+
final FactoryElements<Npc> factoryElements = load(NpcFactory.class, NpcFactoryElements.class, language, moduleName);
60+
if (factoryElements != null && !factoryElements.getElements().isEmpty()) {
61+
return factoryElements.getElements();
62+
}
63+
} catch (InvalidCacheFile invalidCacheFile) {
64+
// Not cache file on this module.
65+
}
66+
return null;
67+
}
68+
69+
@Override
70+
protected FactoryElements<Npc> getFactoryElements(String moduleName, String language) throws InvalidXmlElementException {
71+
return new NpcFactoryElements(language, moduleName);
72+
}
73+
74+
@Override
75+
protected GsonBuilder initGsonBuilder(final String language, final String moduleName) {
76+
final GsonBuilder gsonBuilder = new GsonBuilder();
77+
gsonBuilder.setPrettyPrinting();
78+
gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter(language, moduleName));
79+
gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter(language, moduleName));
80+
gsonBuilder.registerTypeAdapter(Characteristic.class, new CharacteristicAdapter(language, moduleName));
81+
gsonBuilder.registerTypeAdapter(Accessory.class, new AccessoryAdapter(language, moduleName));
82+
gsonBuilder.registerTypeAdapter(DamageType.class, new DamageTypeAdapter(language, moduleName));
83+
gsonBuilder.registerTypeAdapter(Ammunition.class, new AmmunitionAdapter(language, moduleName));
84+
gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter(language, moduleName));
85+
gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter(language, moduleName));
86+
gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter(language, moduleName));
87+
gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter(language, moduleName));
88+
gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter(language, moduleName));
89+
gsonBuilder.registerTypeAdapter(BeneficeDefinition.class, new BeneficeDefinitionAdapter(language, moduleName));
90+
gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter(language, moduleName));
91+
gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter(language, moduleName));
92+
gsonBuilder.registerTypeAdapter(OccultismPath.class, new OccultismPathAdapter(language, moduleName));
93+
gsonBuilder.registerTypeAdapter(IRandomPreference.class, new RandomPreferenceAdapter());
94+
return gsonBuilder;
95+
}
96+
97+
98+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.softwaremagico.tm.cache;
2+
3+
/*-
4+
* #%L
5+
* Think Machine (Rules)
6+
* %%
7+
* Copyright (C) 2017 - 2021 Softwaremagico
8+
* %%
9+
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
10+
* <[email protected]> Valencia (Spain).
11+
*
12+
* This program is free software; you can redistribute it and/or modify it under
13+
* the terms of the GNU General Public License as published by the Free Software
14+
* Foundation; either version 2 of the License, or (at your option) any later
15+
* version.
16+
*
17+
* This program is distributed in the hope that it will be useful, but WITHOUT
18+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20+
* details.
21+
*
22+
* You should have received a copy of the GNU General Public License along with
23+
* this program; If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
24+
* #L%
25+
*/
26+
27+
import com.softwaremagico.tm.InvalidXmlElementException;
28+
import com.softwaremagico.tm.json.factories.FactoryElements;
29+
import com.softwaremagico.tm.json.factories.cache.FactoryCacheLoader;
30+
import com.softwaremagico.tm.random.predefined.characters.Npc;
31+
import com.softwaremagico.tm.random.predefined.characters.NpcFactory;
32+
33+
import java.sql.Timestamp;
34+
import java.util.Date;
35+
36+
public class NpcFactoryElements extends FactoryElements<Npc> {
37+
38+
public NpcFactoryElements() {
39+
super();
40+
creationTime = new Timestamp(new Date().getTime());
41+
}
42+
43+
public NpcFactoryElements(String language, String moduleName) throws InvalidXmlElementException {
44+
this();
45+
creationTime = new Timestamp(new Date().getTime());
46+
47+
//Skip Json generation in loop.
48+
final NpcFactory npcFactory = new NpcFactory() {
49+
@Override
50+
public FactoryCacheLoader<Npc> getFactoryCacheLoader() {
51+
return null;
52+
}
53+
};
54+
55+
setElements(npcFactory.getElements(language, moduleName));
56+
}
57+
}

0 commit comments

Comments
 (0)