Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<checkstyle.version>2.17</checkstyle.version>
<spotbugs.version>4.1.3</spotbugs.version>
<exec-maven-plugin.version>3.0.0</exec-maven-plugin.version>

<gson.cache.folder>${main.basedir}/modules/Fading Suns Revised Edition/json</gson.cache.folder>
</properties>

<build>
Expand Down
43 changes: 43 additions & 0 deletions think-machine-random/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

<properties>
<main.basedir>${basedir}/..</main.basedir>
<gson.cache.tmp.folder>${main.basedir}/think-machine-random/target/classes/Fading Suns Revised Edition/json
</gson.cache.tmp.folder>
</properties>

<build>
Expand All @@ -29,7 +31,48 @@
</suiteXmlFiles>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin><!-- Generates new json cached -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.softwaremagico.tm.cache.FactoryCacheGenerator</mainClass>
<skip>false</skip>
<quietLogs>true</quietLogs>
</configuration>
</plugin>
<!-- Copies and replace json cached -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven-antrun-plugin.version}</version>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<failOnError>false</failOnError>
<target>
<!-- copy json files -->
<copy todir="${gson.cache.folder}" overwrite="true">
<fileset dir="${gson.cache.tmp.folder}" includes="**" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.softwaremagico.tm.cache;

/*-
* #%L
* Think Machine (Rules)
* %%
* Copyright (C) 2017 - 2021 Softwaremagico
* %%
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
* <[email protected]> Valencia (Spain).
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import com.softwaremagico.tm.InvalidXmlElementException;
import com.softwaremagico.tm.file.modules.ModuleManager;
import com.softwaremagico.tm.log.ConfigurationLog;
import com.softwaremagico.tm.log.MachineModulesLog;
import com.softwaremagico.tm.log.MachineXmlReaderLog;
import com.softwaremagico.tm.random.predefined.characters.NpcFactory;
import org.reflections.Reflections;
import org.slf4j.LoggerFactory;

public class FactoryCacheGenerator {

public static void main(String[] args) throws InvalidXmlElementException {
disableLogs();
final NpcFactoryCacheLoader npcFactoryCacheLoader = new NpcFactoryCacheLoader();
for (final String moduleName : ModuleManager.getAvailableModules()) {
npcFactoryCacheLoader.save(NpcFactory.class, moduleName, NpcFactory.getInstance().getTranslatorFile());
}
}

private static void disableLogs() {
Logger logger = (Logger) LoggerFactory.getLogger(MachineXmlReaderLog.class);
logger.setLevel(Level.OFF);
logger = (Logger) LoggerFactory.getLogger(ConfigurationLog.class);
logger.setLevel(Level.OFF);
logger = (Logger) LoggerFactory.getLogger(MachineModulesLog.class);
logger.setLevel(Level.OFF);
logger = (Logger) LoggerFactory.getLogger(Reflections.class);
logger.setLevel(Level.OFF);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.softwaremagico.tm.cache;

/*-
* #%L
* Think Machine (Random Generator)
* %%
* Copyright (C) 2017 - 2021 Softwaremagico
* %%
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
* <[email protected]> Valencia (Spain).
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.softwaremagico.tm.InvalidXmlElementException;
import com.softwaremagico.tm.log.MachineLog;
import com.softwaremagico.tm.random.party.RandomPartyFactory;
import com.softwaremagico.tm.random.predefined.characters.NpcFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

public class ModuleLoaderEnforcer {

public static int loadAllFactories(String language, String moduleName) {
final long startTime = System.nanoTime();
final AtomicInteger loadedElements = new AtomicInteger(0);

loadedElements.addAndGet(com.softwaremagico.tm.file.modules.ModuleLoaderEnforcer.loadAllFactories(language, moduleName));

List<CompletableFuture<Void>> futures = new ArrayList<>();

futures.add(CompletableFuture.runAsync(() -> {
try {
loadedElements.addAndGet(RandomPartyFactory.getInstance().getElements(language, moduleName).size());
} catch (InvalidXmlElementException e) {
MachineLog.errorMessage(com.softwaremagico.tm.file.modules.ModuleLoaderEnforcer.class.getName(), e);
}
}));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
futures = new ArrayList<>();

//Second level
futures.add(CompletableFuture.runAsync(() -> {
try {
loadedElements.addAndGet(NpcFactory.getInstance().getElements(language, moduleName).size());
} catch (InvalidXmlElementException e) {
MachineLog.errorMessage(com.softwaremagico.tm.file.modules.ModuleLoaderEnforcer.class.getName(), e);
}
}));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();


final long duration = (System.nanoTime() - startTime);
MachineLog.info(com.softwaremagico.tm.file.modules.ModuleLoaderEnforcer.class.getName(),
"All factories loaded! Total {} elements loaded in {} milliseconds.",
loadedElements.get(), duration / 1000000);
return loadedElements.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.softwaremagico.tm.cache;

/*-
* #%L
* Think Machine (Rules)
* %%
* Copyright (C) 2017 - 2021 Softwaremagico
* %%
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
* <[email protected]> Valencia (Spain).
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.google.gson.GsonBuilder;
import com.softwaremagico.tm.InvalidXmlElementException;
import com.softwaremagico.tm.character.benefices.AvailableBenefice;
import com.softwaremagico.tm.character.benefices.BeneficeDefinition;
import com.softwaremagico.tm.character.blessings.Blessing;
import com.softwaremagico.tm.character.characteristics.Characteristic;
import com.softwaremagico.tm.character.characteristics.CharacteristicDefinition;
import com.softwaremagico.tm.character.equipment.DamageType;
import com.softwaremagico.tm.character.equipment.armours.Armour;
import com.softwaremagico.tm.character.equipment.shields.Shield;
import com.softwaremagico.tm.character.equipment.weapons.Accessory;
import com.softwaremagico.tm.character.equipment.weapons.Ammunition;
import com.softwaremagico.tm.character.equipment.weapons.Weapon;
import com.softwaremagico.tm.character.factions.Faction;
import com.softwaremagico.tm.character.occultism.OccultismPath;
import com.softwaremagico.tm.character.races.Race;
import com.softwaremagico.tm.character.skills.AvailableSkill;
import com.softwaremagico.tm.json.*;
import com.softwaremagico.tm.json.factories.FactoryElements;
import com.softwaremagico.tm.json.factories.cache.FactoryCacheLoader;
import com.softwaremagico.tm.json.factories.cache.InvalidCacheFile;
import com.softwaremagico.tm.random.predefined.characters.Npc;
import com.softwaremagico.tm.random.predefined.characters.NpcFactory;
import com.softwaremagico.tm.random.selectors.IRandomPreference;

import java.util.List;

public class NpcFactoryCacheLoader extends FactoryCacheLoader<Npc> {

@Override
public List<Npc> load(String language, String moduleName) {
try {
final FactoryElements<Npc> factoryElements = load(NpcFactory.class, NpcFactoryElements.class, language, moduleName);
if (factoryElements != null && !factoryElements.getElements().isEmpty()) {
return factoryElements.getElements();
}
} catch (InvalidCacheFile invalidCacheFile) {
// Not cache file on this module.
}
return null;
}

@Override
protected FactoryElements<Npc> getFactoryElements(String moduleName, String language) throws InvalidXmlElementException {
return new NpcFactoryElements(language, moduleName);
}

@Override
protected GsonBuilder initGsonBuilder(final String language, final String moduleName) {
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Characteristic.class, new CharacteristicAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Accessory.class, new AccessoryAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(DamageType.class, new DamageTypeAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Ammunition.class, new AmmunitionAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(BeneficeDefinition.class, new BeneficeDefinitionAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(OccultismPath.class, new OccultismPathAdapter(language, moduleName));
gsonBuilder.registerTypeAdapter(IRandomPreference.class, new RandomPreferenceAdapter());
return gsonBuilder;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.softwaremagico.tm.cache;

/*-
* #%L
* Think Machine (Rules)
* %%
* Copyright (C) 2017 - 2021 Softwaremagico
* %%
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
* <[email protected]> Valencia (Spain).
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.softwaremagico.tm.InvalidXmlElementException;
import com.softwaremagico.tm.json.factories.FactoryElements;
import com.softwaremagico.tm.json.factories.cache.FactoryCacheLoader;
import com.softwaremagico.tm.random.predefined.characters.Npc;
import com.softwaremagico.tm.random.predefined.characters.NpcFactory;

import java.sql.Timestamp;
import java.util.Date;

public class NpcFactoryElements extends FactoryElements<Npc> {

public NpcFactoryElements() {
super();
creationTime = new Timestamp(new Date().getTime());
}

public NpcFactoryElements(String language, String moduleName) throws InvalidXmlElementException {
this();
creationTime = new Timestamp(new Date().getTime());

//Skip Json generation in loop.
final NpcFactory npcFactory = new NpcFactory() {
@Override
public FactoryCacheLoader<Npc> getFactoryCacheLoader() {
return null;
}
};

setElements(npcFactory.getElements(language, moduleName));
}
}
Loading