Exemple #1
0
        private bool LoadContract(string targetPath)
        {
            targetPath = targetPath.Replace("\\", "/");
            projectFiles.Clear();

            activeDocumentID = null;

            var extension = Path.GetExtension(targetPath);

            if (extension != ".avm")
            {
                var language = LanguageSupport.DetectLanguage(targetPath);
                if (language == SourceLanguage.Other)
                {
                    return(false);
                }

                var mainEntry = LoadFile(targetPath);
                if (mainEntry == null)
                {
                    return(false);
                }

                var sourceCode = mainEntry.content;

                var avmFilePath = targetPath.Replace(extension, ".avm");

                if (!File.Exists(avmFilePath))
                {
                    if (!_shell.Debugger.CompileContract(sourceCode, language, avmFilePath))
                    {
                        return(false);
                    }
                }

                if (!_shell.Debugger.LoadContract(avmFilePath))
                {
                    return(false);
                }

                LoadFile(avmFilePath, _shell.Debugger.avmDisassemble.ToString());
            }

            return(true);
        }
Exemple #2
0
        public void LoadDataFromFile(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (!File.Exists(path))
            {
                MessageBox.Show("Can't find '" + (String.IsNullOrEmpty(path) ? "(null)" : path + "'"), this.Text + " - " + Environment.CurrentDirectory);
            }
            else
            {
                MainForm.targetAVMPath = path;

                this.contractName = Path.GetFileNameWithoutExtension(path);

                this.contractBytecode = File.ReadAllBytes(path);

                var oldMapFileName = path.Replace(".avm", ".neomap");
                var newMapFileName = path.Replace(".avm", ".debug.json");

                debugMode      = DebugMode.Assembly;
                sourceLanguage = SourceLanguageKind.Other;

                if (File.Exists(newMapFileName))
                {
                    map = new NeoMapFile();
                    map.LoadFromFile(newMapFileName, contractBytecode);

                    this.contractName = map.contractName;
                }
                else
                {
                    if (File.Exists(oldMapFileName))
                    {
                        MessageBox.Show("Warning: The file format of debug map changed. Please recompile your AVM with the latest compiler.");
                    }
                    map = null;
                }

                string abiFilename = path.Replace(".avm", ".abi.json");
                if (File.Exists(abiFilename))
                {
                    abi = new ABI(abiFilename);
                }
                else
                {
                    MessageBox.Show($"Error: {abiFilename} was not found. Please recompile your AVM with the latest compiler.");
                    return;
                }

                this.debugger = null;
                this.avm_asm  = NeoDisassembler.Disassemble(contractBytecode);

                if (map != null && map.Entries.Any())
                {
                    var srcFile = map.Entries.FirstOrDefault().url;

                    if (string.IsNullOrEmpty(srcFile))
                    {
                        MessageBox.Show("Error: Could not load the debug map correct, no file entries.");
                        return;
                    }

                    if (!File.Exists(srcFile))
                    {
                        MessageBox.Show("Error: Could not load the source code, check that this file exists:" + srcFile);
                        return;
                    }

                    FileName.Text = srcFile;

                    sourceLanguage = LanguageSupport.DetectLanguage(srcFile);

                    debugMode = DebugMode.Source;
                    debugContent[DebugMode.Source] = File.ReadAllText(srcFile);
                }

                debugContent[DebugMode.Assembly] = avm_asm.ToString();
                FileName.Text = Path.GetFileName(path);

                ReloadTextArea();

                BlockchainLoad();

                UpdateSourceViewMenus();

                shouldReset = true;

                settings.lastOpenedFile = path;
                settings.Save();
            }
        }
        public bool LoadAvmFile(string avmPath)
        {
            //Decide what we need to open
            if (!String.IsNullOrEmpty(avmPath)) //use the explicit file provided
            {
                _avmFilePath = avmPath;
            }
            else if (!String.IsNullOrEmpty(_settings.lastOpenedFile)) //fallback to last opened
            {
                _avmFilePath = _settings.lastOpenedFile;
            }
            else
            {
                return(false); //We don't know what to open, just let the user specify with another call
            }
            //Housekeeping - let's find out what files we have and make sure we're good
            if (!File.Exists(_avmFilePath))
            {
                Log("File not found. " + avmPath);
                return(false);
            }
            else if (File.Exists(_oldMapFilePath))
            {
                Log("Old map file format found.  Please recompile your avm with the latest compiler.");
                return(false);
            }

            _debugContent     = new Dictionary <DebugMode, string>();
            _mode             = DebugMode.Assembly;
            _language         = SourceLanguage.Other;
            _contractName     = Path.GetFileNameWithoutExtension(_avmFilePath);
            _contractByteCode = File.ReadAllBytes(_avmFilePath);
            _map = new NeoMapFile();
            try
            {
                _avmAsm = NeoDisassembler.Disassemble(_contractByteCode);
            }
            catch (DisassembleException e)
            {
                Log($"Disassembler Error: {e.Message}");
                return(false);
            }

            if (File.Exists(_abiFilePath))
            {
                _aBI = new ABI(_abiFilePath);
            }
            else
            {
                _aBI = new ABI();
                Log($"Warning: {_abiFilePath} was not found. Please recompile your AVM with the latest compiler.");
            }

            //We always should have the assembly content
            _debugContent[DebugMode.Assembly] = _avmAsm.ToString();

            //Let's see if we have source code we can map
            if (File.Exists(_mapFilePath))
            {
                _map.LoadFromFile(_mapFilePath, _contractByteCode);
            }
            else
            {
                _map = null;
                Log($"Warning: Could not find {_mapFilePath}");
                //return false;
            }

            if (_map != null && _map.Entries.Any())
            {
                _srcFileName = _map.Entries.FirstOrDefault().url;
                if (string.IsNullOrEmpty(_srcFileName))
                {
                    throw new Exception("Error: Could not load the debug map correctly, no source file specified in map.");
                }
                if (!File.Exists(_srcFileName))
                {
                    throw new Exception($"Error: Could not load the source code, check that this file exists: {_srcFileName}");
                }

                _debugContent[DebugMode.Source] = File.ReadAllText(_srcFileName);
                _language = LanguageSupport.DetectLanguage(_srcFileName);
                _mode     = DebugMode.Source;
            }

            //Save the settings
            _settings.lastOpenedFile = avmPath;
            _settings.Save();
            _avmFileLoaded = true;

            //Force a reset now that we're loaded
            _resetFlag = true;

            return(true);
        }