001package com.fs.starfarer.api.impl.campaign.rulecmd;
002
003import java.awt.Color;
004import java.util.ArrayList;
005import java.util.Collections;
006import java.util.List;
007import java.util.Map;
008
009import com.fs.starfarer.api.Global;
010import com.fs.starfarer.api.campaign.InteractionDialogAPI;
011import com.fs.starfarer.api.campaign.rules.MemKeys;
012import com.fs.starfarer.api.campaign.rules.MemoryAPI;
013import com.fs.starfarer.api.impl.campaign.DebugFlags;
014import com.fs.starfarer.api.util.Misc;
015import com.fs.starfarer.api.util.Misc.Token;
016
017/**
018 * Usage: AbortWait $waitHandle
019 * 
020 * @author Alex Mosolov
021 *
022 * Copyright 2014 Fractal Softworks, LLC
023 */
024public class DumpMemory extends BaseCommandPlugin {
025
026        public static final String OPTION_ID = "DumpMemory.option_dump_memory";
027        
028        public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
029                //if (dialog == null) return false;
030                
031                //System.out.println("Transponder: " + dialog.getInteractionTarget().isTransponderOn());
032                
033                List<String> memKeys = new ArrayList<String>(memoryMap.keySet());
034                Collections.sort(memKeys);
035                memKeys.remove(MemKeys.LOCAL);
036                memKeys.add(MemKeys.LOCAL);
037                
038                Color HIGHLIGHT_COLOR = Global.getSettings().getColor("buttonShortcut");
039                Color GRAY_COLOR = new Color(100,100,100);
040                
041                
042                for (String memKey : memKeys) {
043                        String text = "";
044                        MemoryAPI memory = memoryMap.get(memKey);
045                        //text += memKey.toUpperCase() + "\n";
046                        List<String> keys = new ArrayList<String>(memory.getKeys());
047                        Collections.sort(keys);
048                        
049                        List<Color> highlightColors = new ArrayList<Color>();
050                        List<String> highlightList = new ArrayList<String>();
051                        for (String key : keys) {
052                                Object value = memory.get(key);
053                                
054                                String varName = "$" + memKey + ".";
055                                if (memKey.equals(MemKeys.LOCAL)) {
056                                        varName = "$";
057                                }
058                                if (key.startsWith("$")) {
059                                        varName += key.substring(1);
060                                        //highlightList.add(key.substring(1));
061                                } else {
062                                        varName += key;
063                                        //highlightList.add(key);
064                                }
065                                
066                                if (varName.length() > 35) {
067                                        varName = varName.substring(0, 35) + "...";
068                                }
069                                
070                                highlightColors.add(HIGHLIGHT_COLOR);
071                                highlightList.add(varName);
072
073                                text += varName;
074                                if (value instanceof Boolean || value instanceof String || value instanceof Float || value instanceof Integer || value instanceof Long) {
075                                        text += " = " + value.toString();
076                                } else if (value != null) {
077                                        text += " = " + value.getClass().getSimpleName() + "@" + value.hashCode();
078                                } else {
079                                        text += " = " + "null";
080                                }
081                                float expire = memory.getExpire(key);
082                                if (expire >= 0) {
083                                        String eText = "(e=" + (float)((int)(expire * 10)/10f) + ")";
084                                        if (expire == 0) {
085                                                eText = "(e=0)";
086                                        }
087                                        highlightColors.add(GRAY_COLOR);
088                                        highlightList.add(eText);
089                                        text += " " + eText;
090                                }
091                                text += "\n";
092                        }
093                        if (dialog != null) {
094                                //dialog.getTextPanel().setFontSmallInsignia();
095                                dialog.getTextPanel().addParagraph(text);
096                                dialog.getTextPanel().setHighlightColorsInLastPara(highlightColors.toArray(new Color[0]));
097                                dialog.getTextPanel().highlightInLastPara(highlightList.toArray(new String [0]));
098                                //dialog.getTextPanel().setFontInsignia();
099                        } else {
100                                if (DebugFlags.PRINT_RULES_DEBUG_INFO) {
101                                        System.out.println(text);       
102                                }
103                        }
104                        //dialog.getTextPanel().highlightInLastPara(HIGHLIGHT_COLOR, highlightList.toArray(new String [0]));
105                }
106                
107                return true;
108        }
109        
110        public static void addOption(InteractionDialogAPI dialog) {
111                dialog.getOptionPanel().addOption(">> (dev) dump memory", DumpMemory.OPTION_ID, Misc.getGrayColor(), null);
112        }
113}
114
115
116
117
118