ExternalCodeTableGetter.java
Upload User: gwt600
Upload Date: 2021-06-03
Package Size: 704k
Code Size: 4k
Category:

Games

Development Platform:

Java

  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> 
  4. Matthias Butz <matze@odinms.de>
  5. Jan Christian Meyer <vimes@odinms.de>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License version 3
  8. as published by the Free Software Foundation. You may not use, modify
  9. or distribute this program under any other version of the
  10. GNU Affero General Public License.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18. package net.sf.odinms.net;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Collections;
  22. import java.util.Comparator;
  23. import java.util.List;
  24. import java.util.Properties;
  25. import net.sf.odinms.tools.HexTool;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. public class ExternalCodeTableGetter {
  29.     Properties props;
  30.     public ExternalCodeTableGetter(Properties properties) {
  31.         props = properties;
  32.     }
  33.     private static <T extends Enum<? extends IntValueHolder> & IntValueHolder> T valueOf(String name, T[] values) {
  34.         for (T val : values) {
  35.             if (val.name().equals(name)) {
  36.                 return val;
  37.             }
  38.         }
  39.         return null;
  40.     }
  41.     private <T extends Enum<? extends IntValueHolder> & IntValueHolder> int getValue(String name, T[] values, int def) {
  42.         String prop = props.getProperty(name);
  43.         if (prop != null && prop.length() > 0) {
  44.             String trimmed = prop.trim();
  45.             String[] args = trimmed.split(" ");
  46.             int base = 0;
  47.             String offset;
  48.             if (args.length == 2) {
  49.                 base = valueOf(args[0], values).getValue();
  50.                 if (base == def) {
  51.                     base = getValue(args[0], values, def);
  52.                 }
  53.                 offset = args[1];
  54.             } else {
  55.                 offset = args[0];
  56.             }
  57.             if (offset.length() > 2 && offset.substring(0, 2).equals("0x")) {
  58.                 return Integer.parseInt(offset.substring(2), 16) + base;
  59.             } else {
  60.                 return Integer.parseInt(offset) + base;
  61.             }
  62.         }
  63.         return def;
  64.     }
  65.     public static <T extends Enum<? extends WritableIntValueHolder> & WritableIntValueHolder> String getOpcodeTable(T[] enumeration) {
  66.         StringBuilder enumVals = new StringBuilder();
  67.         List<T> all = new ArrayList<T>(); // need a mutable list plawks
  68.         all.addAll(Arrays.asList(enumeration));
  69.         Collections.sort(all, new Comparator<IntValueHolder>() {
  70.             @Override
  71.             public int compare(IntValueHolder o1, IntValueHolder o2) {
  72.                 return Integer.valueOf(o1.getValue()).compareTo(o2.getValue());
  73.             }
  74.         });
  75.         for (T code : all) {
  76.             enumVals.append(code.name());
  77.             enumVals.append(" = ");
  78.             enumVals.append("0x");
  79.             enumVals.append(HexTool.toString(code.getValue()));
  80.             enumVals.append(" (");
  81.             enumVals.append(code.getValue());
  82.             enumVals.append(")n");
  83.         }
  84.         return enumVals.toString();
  85.     }
  86.     public static <T extends Enum<? extends WritableIntValueHolder> & WritableIntValueHolder> void populateValues(Properties properties, T[] values) {
  87.         ExternalCodeTableGetter exc = new ExternalCodeTableGetter(properties);
  88.         for (T code : values) {
  89.             code.setValue(exc.getValue(code.name(), values, -2));
  90.         }
  91.         Logger log = LoggerFactory.getLogger(ExternalCodeTableGetter.class);
  92.         if (log.isTraceEnabled()) { // generics - copy pasted between send and recv current?
  93.             log.trace(getOpcodeTable(values));
  94.         }
  95.     }
  96. }