// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 6/15/2001 17:43:14
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   com/macromedia/generator/app/FlashEnvironment

package com.macromedia.generator.app;

import com.macromedia.generator.api.*;
import com.macromedia.generator.commands.SetEnvironment2Command;
import com.macromedia.generator.commands.SetEnvironmentCommand;
import com.macromedia.generator.platform.Mac;
import com.macromedia.generator.utils.GenInfo;
import java.io.*;
import java.util.*;

// Referenced classes of package com.macromedia.generator.app:
//            CacheManager, CommandProperties, Expired, FlashCommandException, 
//            FlashCommandLog, FontCache, GenCache, GenericCommand, 
//            Invalid, InvalidCommandException, Logo, MediaCache

public class FlashEnvironment extends Hashtable
    implements FlashCommandLog
{

    public FlashEnvironment(int i, boolean flag, Serial serial, String s, CacheManager cachemanager)
    {
        E2 = null;
        E3 = null;
        E4 = 1;
        E9 = false;
        EA = null;
        EB = Expired.expiredData;
        EC = Invalid.invalidData;
        ED = null;
        EF = null;
        E4 = i;
        E5 = s;
        DF = flag;
        DC = serial;
        E1 = cachemanager;
        E7 = new StringWriter();
        E6 = new PrintWriter(E7);
        DA();
        DA = false;
        ED = null;
        if(serial != null)
            EE = serial.getLimited();
    }

    private void DA()
    {
        if(E1 == null || !E1.useInternalCache())
            return;
        long l = 0L;
        if(E1.getFontCache() != null && !E1.getFontCache().isCacheEnabled())
        {
            long l1 = E1.getInternalFontCacheMaxSize();
            E2 = new FontCache(l1, false, true, 0L, true, 0, 0, null);
        }
        if(E1.getMediaCache() != null && !E1.getMediaCache().isCacheEnabled())
        {
            long l2 = E1.getInternalMediaCacheMaxSize();
            E3 = new MediaCache(l2, false, true, 0L, true, 0, 0, null);
        }
    }

    private void DB()
    {
        if(E2 != null)
        {
            E2.dispose();
            E2 = null;
        }
        if(E3 != null)
        {
            E3.dispose();
            E3 = null;
        }
    }

    protected void finalize()
        throws Throwable
    {
        DB();
    }

    private synchronized Class DC(String s, FlashEnvironment flashenvironment)
    {
        if(E8 == null)
            E8 = new Hashtable();
        try
        {
            Class class1 = (Class)E8.get(s);
            if(class1 == null)
            {
                class1 = Class.forName(s);
                E8.put(s, class1);
            }
            return class1;
        }
        catch(ClassNotFoundException _ex)
        {
            ResourceBundle resourcebundle = ResourceBundle.getBundle("com.macromedia.generator.resources.ResourceData", Locale.getDefault());
            flashenvironment.logError(resourcebundle.getString("FlashCommand") + s + ": " + resourcebundle.getString("TemplateNotFound"));
        }
        catch(ClassCastException _ex)
        {
            ResourceBundle resourcebundle1 = ResourceBundle.getBundle("com.macromedia.generator.resources.ResourceData", Locale.getDefault());
            flashenvironment.logError(resourcebundle1.getString("FlashCommand") + s + ": " + resourcebundle1.getString("TemplateCast"));
        }
        catch(Exception exception)
        {
            ResourceBundle resourcebundle2 = ResourceBundle.getBundle("com.macromedia.generator.resources.ResourceData", Locale.getDefault());
            flashenvironment.logError(resourcebundle2.getString("FlashCommand") + s + ": " + exception.getMessage());
        }
        return null;
    }

    public CacheManager getCacheManager()
    {
        return E1;
    }

    public String getDataEncoding()
    {
        return DE;
    }

    public Script getDefaultSymbolScript()
    {
        return EA;
    }

    public File getDirectory()
    {
        return DB;
    }

    public String getExternalFontLocation()
    {
        return E5;
    }

    public FontCache getFontCache()
    {
        return E2;
    }

    public GenInfo getGenInfo()
    {
        if(ED == null)
        {
            ED = new GenInfo();
            ED.setEnvironment(this);
        }
        return ED;
    }

    public PrintWriter getLogWriter()
    {
        return E6;
    }

    public MediaCache getMediaCache()
    {
        return E3;
    }

    public String getMessages()
    {
        return E7.toString();
    }

    public boolean getPreviewMode()
    {
        return DF;
    }

    public String getResString(String s)
    {
        ResourceBundle resourcebundle = ResourceBundle.getBundle("com.macromedia.generator.resources.ResourceData", Locale.getDefault());
        return resourcebundle.getString(s);
    }

    public Script getTemplate()
    {
        return DD;
    }

    public static boolean isLimited()
    {
        return EE;
    }

    public Script loadDefaultSymbols()
    {
        String s = null;
        File file = null;
        s = System.getProperty("com.macromedia.generator.defaultSymbolFile");
        if(s != null)
            file = DD(s);
        if(file == null)
        {
            String s2 = System.getProperty("com.macromedia.generator.installpath");
            String s3 = System.getProperty("file.separator");
            if(s2 != null)
            {
                if(s != null)
                {
                    s = s2 + s3 + "bin" + s3 + s;
                    file = DD(s);
                }
                if(file == null)
                {
                    String s1 = s2 + s3 + "bin" + s3 + "DefaultSymbolFile.swt";
                    file = DD(s1);
                }
            }
        }
        if(file == null || !file.exists() || !file.isFile())
        {
            EA = null;
        } else
        {
            EA = new Script();
            try
            {
                EA.setFile(file.getPath(), true);
            }
            catch(GeneratorApiException _ex)
            {
                EA = null;
            }
        }
        return EA;
    }

    public void logError(String s)
    {
        if(E4 > 0)
            E6.println("\t" + s);
    }

    public void logError(String s, String s1, String s2)
    {
        if(E4 > 0)
        {
            ResourceBundle resourcebundle = ResourceBundle.getBundle(s, Locale.getDefault());
            E6.println("\t" + resourcebundle.getString(s1) + (s2 == null ? "" : s2));
        }
    }

    public int logLevel()
    {
        return E4;
    }

    public void logMessage(String s)
    {
        if(E4 > 2)
            E6.println("\t" + s);
    }

    public void logMessage(String s, String s1, String s2)
    {
        if(E4 > 2)
        {
            ResourceBundle resourcebundle = ResourceBundle.getBundle(s, Locale.getDefault());
            E6.println("\t" + resourcebundle.getString(s1) + (s2 == null ? "" : s2));
        }
    }

    public void logWarning(String s)
    {
        if(E4 > 1)
            E6.println("\t" + s);
    }

    public void logWarning(String s, String s1, String s2)
    {
        if(E4 > 1)
        {
            ResourceBundle resourcebundle = ResourceBundle.getBundle(s, Locale.getDefault());
            E6.println("\t" + resourcebundle.getString(s1) + (s2 == null ? "" : s2));
        }
    }

    private File DD(String s)
    {
        File file = null;
        Mac mac = Mac.getMac();
        if(mac != null)
        {
            String s1 = mac.makeJavaPretty(s);
            file = new File(s1);
            if(!file.exists() || !file.isFile())
                file = new File(s1.substring(1));
        } else
        {
            file = new File(s);
        }
        if(file == null)
            return file;
        if(!file.exists() || !file.isFile())
            return null;
        else
            return file;
    }

    public void processScript(Script script, Context context)
    {
        E0++;
        if(E0 < 50)
        {
            Context context1;
            if(script == DD)
            {
                context1 = context;
            } else
            {
                context1 = new Context();
                context1.setParent(context);
            }
            int i = script.getCommandCount();
            boolean flag = false;
            for(int j = 0; j < i; j++)
            {
                String s = script.getCommandString(j);
                try
                {
                    CommandProperties commandproperties = new CommandProperties();
                    commandproperties.parseCommand(s);
                    Class class1 = DC(commandproperties.getProperty("command"), this);
                    if(class1 == null)
                        continue;
                    GenericCommand genericcommand = (GenericCommand)class1.newInstance();
                    if(genericcommand == null)
                        continue;
                    long l1 = System.currentTimeMillis();
                    if((genericcommand instanceof SetEnvironmentCommand) || (genericcommand instanceof SetEnvironment2Command))
                    {
                        if(flag)
                            continue;
                        flag = true;
                    }
                    genericcommand.doCommand(this, script, context1, j, commandproperties);
                }
                catch(InstantiationException _ex)
                {
                    logError(getResString("CommandProcess") + j + ": " + s);
                }
                catch(IllegalAccessException _ex)
                {
                    logError(getResString("CommandProcess") + j + ": " + s);
                }
                catch(FlashCommandException _ex)
                {
                    logError(getResString("CommandProcess") + j + ": " + s);
                }
                catch(InvalidCommandException _ex)
                {
                    logError(getResString("CommandProcess") + j + ": " + s);
                }
            }

            int k = script.getScriptCount();
            for(int l = 0; l < k; l++)
            {
                Script script1 = script.getScript(l);
                if(!script1.getIsCommand() && !script1.getIsProcessed())
                    processScript(script1, context1);
                script1.release();
            }

            script.applyContext(context1);
            if(context1 != context)
                context1.release();
        } else
        {
            logError(getResString("InfiniteLoop"));
        }
        script.setIsProcessed(true);
        E0--;
    }

    public Buffer processTemplate(File file, Vector vector)
    {
        if(DA)
        {
            logError(getResString("InvalidCall"));
            return null;
        }
        int i = DC.flush(DF);
        if(DF)
            i = 0;
        Buffer buffer;
        if(i == 1)
        {
            logError(getResString("SerialExpired"));
            try
            {
                buffer = new Buffer();
                buffer.write(EB);
            }
            catch(GeneratorApiException _ex)
            {
                logError(getResString("MemWriteErr"));
                return null;
            }
        } else
        if(i == 2)
        {
            logError(getResString("SerialInvalid"));
            try
            {
                buffer = new Buffer();
                buffer.write(EC);
            }
            catch(GeneratorApiException _ex)
            {
                logError(getResString("MemWriteErr"));
                return null;
            }
        } else
        {
            DA = true;
            Context context = new Context();
            String as[];
            for(Enumeration enumeration = vector.elements(); enumeration.hasMoreElements(); context.setValue(as[0], as[1]))
                as = (String[])enumeration.nextElement();

            if(!file.exists() || !file.isFile())
            {
                logError(getResString("LoadTempError") + file.getAbsolutePath());
                context.release();
                DA = false;
                return null;
            }
            File file1 = new File(file.getAbsolutePath());
            String s = file1.getParent();
            if(s == null)
            {
                logError(getResString("TempPathErr"));
                context.release();
                DA = false;
                return null;
            }
            DB = new File(s);
            try
            {
                DD = new Script();
                DD.setFile(file1.getPath(), DF ^ true);
                DD.setSerial(DC);
            }
            catch(GeneratorApiException _ex)
            {
                logError(getResString("TempCreateErr"));
                context.release();
                DA = false;
                return null;
            }
            try
            {
                if(E1 != null)
                    E1.loadExternalFonts(this, DD);
            }
            catch(GeneratorApiException _ex)
            {
                context.release();
                DA = false;
                return null;
            }
            E0 = 0;
            processScript(DD, context);
            if(EF != null && EF.length() > 0)
                try
                {
                    DD.addTag(EF);
                }
                catch(GeneratorApiException _ex)
                {
                    logError(getResString("EmbedStringErr"));
                }
            if(i == 3)
                try
                {
                    int j = DD.getLastFrame();
                    Buffer buffer1 = new Buffer();
                    buffer1.write(Logo.logoData);
                    Script script = new Script();
                    script.setBuffer(buffer1);
                    script.insertFrames(0, j);
                    int k = DD.getWidth();
                    int l = DD.getHeight();
                    int i1 = script.getWidth();
                    int j1 = script.getHeight();
                    long l1 = System.currentTimeMillis() / 100L;
                    String s1 = Long.toString(l1);
                    int k1 = (int)(l1 - (l1 / 5L) * 5L);
                    int i2 = 100;
                    int j2 = 100;
                    int k2 = i1 / 2;
                    int l2 = j1 / 2;
                    Rect rect = DD.getBounds();
                    switch(k1)
                    {
                    case 0: // '\0'
                    default:
                        i2 = rect.getXMin() + k2;
                        j2 = rect.getYMin() + l2;
                        break;

                    case 1: // '\001'
                        i2 = rect.getXMax() - k2;
                        j2 = rect.getYMin() + l2;
                        break;

                    case 2: // '\002'
                        i2 = rect.getXMax() - k2;
                        j2 = rect.getYMax() - l2;
                        break;

                    case 3: // '\003'
                        i2 = rect.getXMin() + k2;
                        j2 = rect.getYMax() - l2;
                        break;

                    case 4: // '\004'
                        i2 = (rect.getXMin() + rect.getXMax() >> 1) - 4 * k2;
                        j2 = (rect.getYMin() + rect.getYMax() >> 1) - 4 * l2;
                        break;
                    }
                    float f = 1.0F;
                    float f1 = 1.0F;
                    Matrix matrix = new Matrix();
                    matrix.setXOffset(i2);
                    matrix.setYOffset(j2);
                    matrix.setXScale(f);
                    matrix.setYScale(f1);
                    DD.insertObject(script, 0, 0, matrix, j, matrix, null, s1);
                    script.release();
                    matrix.release();
                    buffer1.release();
                }
                catch(GeneratorApiException _ex)
                {
                    logError(getResString("InsertMarkErr"));
                    context.release();
                    DA = false;
                    return null;
                }
            try
            {
                buffer = DD.writeBuffer();
            }
            catch(GeneratorApiException _ex)
            {
                logError(getResString("MemWriteErr"));
                context.release();
                DA = false;
                return null;
            }
            DD.release();
            DD = null;
            DB = null;
            context.release();
            DA = false;
        }
        return buffer;
    }

    public void setDataEncoding(String s)
    {
        DE = s;
    }

    public void setDefaultSymbolScript(Script script)
    {
        EA = script;
    }

    public void setEmbeddedString(String s)
    {
        EF = s;
    }

    private boolean DA;
    private File DB;
    private Serial DC;
    private Script DD;
    private String DE;
    private boolean DF;
    private int E0;
    private CacheManager E1;
    private FontCache E2;
    private MediaCache E3;
    private int E4;
    private String E5;
    private PrintWriter E6;
    private StringWriter E7;
    private Hashtable E8;
    private boolean E9;
    private Script EA;
    private byte EB[];
    private byte EC[];
    private GenInfo ED;
    private static boolean EE = false;
    private String EF;

}