Jump to content

What is wrong with this code?


neotropic

Recommended Posts

This is FOMM install script code. I use the same code on two other mods. But someone is getting an exception error with this one. And I am curious as to why.

Other than changimg
class Script : FalloutNewVegasBaseScript

to

class Script : BaseScript

 

And would that cause it?

 

What else?

Thanks.

using System;
using fomm.Scripting;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;

class Script : FalloutNewVegasBaseScript {
    public static bool install; 
    
    public static String title = "Neotropics Targeting HUD";
    
    static ASCIIEncoding encoding;
    
    public static Form installStartForm; 
    public static PictureBox backgroundPicture; 
    public static Button okButton;  
    public static Button cancelButton;
 
    
    public static Image GetImageFromFomod(string filename) {  
        byte[] data = GetFileFromFomod(filename);  
        MemoryStream s = new MemoryStream(data);  
        Image img = Image.FromStream(s);  
        s.Close();  
        
        return img;  
    } 
    
    static bool InstallFileIfNotPresent(string path)
	{
		if (! DataFileExists(path)) {
			return InstallFileFromFomod(path);
			
		}
		
		return false;
    }
    
	static bool UpdateInclude(string path, string includePath)
	{
		bool editSuccess = AppendInclude(path, includePath);
	
		if (! editSuccess) {
			MessageBox("Failed to access " + path + ". Reinstall the mod with all other applications closed, or try a manual installation (see readme).", title);
		}
		
		return editSuccess;
    }
    
    static bool AppendInclude(string xmlPath, string includePath)
	{
		byte[] data = GetExistingDataFile(xmlPath);
		
		if (data == null)
			return false;
		
		string tmp = encoding.GetString(data);
		
		// Include is already there?
		if (Regex.Match(tmp, "<include src=\"" + Regex.Escape(includePath) + "\" />", RegexOptions.Singleline).Success == true)
			return true;
		
		tmp += "\r\n\r\n"
			+ "<!-- BEGIN Added by " + title + " -->\r\n"
			+ "<include src=\"" + includePath + "\" />\r\n"
			+ "<!-- END Added by " + title + " -->\r\n";
		
		data = encoding.GetBytes(tmp);
			
		GenerateDataFile(xmlPath, data);
		
		return true;
	}
	
	// Installs include from fomod if not present, appends editString otherwise
	static bool UpdateUIFile(string path, string srcPath, string includePath)
	{
		if (! DataFileExists(path)) {
			return InstallFileFromFomod(srcPath, path);
		} else {

			bool editSuccess = AppendIncludeToMenu(path, includePath);
		
			if (! editSuccess) {
				MessageBox("Failed to access " + path + ". Reinstall the mod with all other applications closed, or try a manual installation (see readme).", title);
			}
			
			return editSuccess;
		}
    }
    
    static bool AppendIncludeToMenu(string xmlPath, string includePath)
	{
		byte[] data = GetExistingDataFile(xmlPath);
		
		if (data == null)
			return false;
			
		string tmp = encoding.GetString(data);

		// Include is already there?
		if (Regex.Match(tmp, "<include src=\"" + Regex.Escape(includePath) + "\" />", RegexOptions.Singleline).Success == true)
			return true;
		
		string includeStr = "\r\n"
			+ "\t<!-- BEGIN Added by " + title + " -->\r\n"
			+ "\t<include src=\"" + includePath + "\" />\r\n"
			+ "\t<!-- END Added by " + title + " -->\r\n";
		
		tmp = Regex.Replace(tmp,
			"<menu name=\"(\\w+)\">(.*)</menu>\\s*$",
			"<menu name=\"$1\">$2" + includeStr + "</menu>",
			RegexOptions.Singleline);
		
		data = encoding.GetBytes(tmp);
			
		GenerateDataFile(xmlPath, data);
		
		return true;
	}
	
    static bool InstallFileFromFomod(string source, string target)
	{
		byte[] data = GetFileFromFomod(source);
		if (data == null)
			return false;
		
		return GenerateDataFile(target, data);
	}
    
    public static void CreateStartForm() {  
		setUpStartForm();
		setUpStartBackgroundImage();
 		setUpButtons();
        AttachHandlers();  
    }

    
    public static void setUpStartForm() {
        installStartForm = CreateCustomForm(); 
        
        installStartForm.FormBorderStyle = FormBorderStyle.Fixed3D;  
        installStartForm.StartPosition = FormStartPosition.CenterScreen;  
        installStartForm.Text = title;
        installStartForm.Size = new Size(500, 250);  
    }
    
    public static void setUpStartBackgroundImage() {
        backgroundPicture = new PictureBox();  
        backgroundPicture.Location = new Point(0, 0);  
        backgroundPicture.Size = new Size(500, 250);  

        // load picture file from .fomod and stream into picture box  
        backgroundPicture.Image = GetImageFromFomod("fomod/banner.png");  

        // add BackgroundPicture to list of controls  
        installStartForm.Controls.Add(backgroundPicture);
    }
    
        
    public static void setUpButtons() {
        okButton = new Button();  
        okButton.Text = "Install";  
        okButton.BackColor = Color.Silver;  
        okButton.Location = new Point(335, 165);  
        okButton.Size = new Size(130, 33);  

        // cancel button  
        cancelButton = new Button();  
        cancelButton.Text = "Cancel";  
        cancelButton.BackColor = Color.Silver;  
        cancelButton.Location = new Point(25, 165);  
        cancelButton.Size = new Size(130, 33);  

        backgroundPicture.Controls.Add(okButton);  
        backgroundPicture.Controls.Add(cancelButton);     
    }
      
    
    public static void AttachHandlers() {  

        //Attach a handler that will fire when the apply button is clicked  
        okButton.Click += delegate(object sender, EventArgs args) {  
            install = true;  
            installStartForm.Close(); 
        };  

        cancelButton.Click += delegate(object sender, EventArgs args) {  
            install = false;  
            installStartForm.Close();  
        };  
    } 


	public static bool OnActivate() {
		encoding = new ASCIIEncoding();
	
		bool GetNVSE = ScriptExtenderPresent();
		Version CurrentNVSE = GetNvseVersion();
		Version RequiredNVSE = new Version(0, 2, 0, 12);
		
        if (CurrentNVSE < RequiredNVSE) {
			if (GetNVSE == false) {
	            MessageBox("NVSE not detected. This mod requires NVSE v " + RequiredNVSE + " or higher. Please update fromhttp://fose.silverlock.org/");
	            return false;
			}
			else {
	            MessageBox("NVSE not outdetected. This mod requires NVSE v " + RequiredNVSE + " or higher. You have "
	                    + CurrentNVSE + ". Please update from http://fose.silverlock.org/");
	            return false;
			}
		}		
		CreateStartForm();
		installStartForm.ShowDialog(); 
				
		if (install) {
	        InstallMain();
	        InstallMenu();
		}
		
		return install;
	}

	
	public static void InstallMain() {
		InstallFileFromFomod("NeotropicsTarget.esp");
		SetPluginActivation("NeotropicsTarget.esp", true);	
	}
	public static void InstallMenu() {	
		InstallFileIfNotPresent("menus/main/hud_main_menu.xml");
		InstallFileIfNotPresent("menus/prefabs/includes_HUDMainMenu.xml");
		CopyDataFile("menus/prefabs/Neotropic/Neotropic_TargetHUD.xml", "menus/prefabs/Neotropic/Neotropic_TargetHUD.xml");		

		UpdateInclude("menus/prefabs/includes_HUDMainMenu.xml", "Neotropic/Neotropic_TargetHUD.xml");	
		UpdateUIFile("menus/main/hud_main_menu.xml", "menus/main/hud_main_menu.xml", "includes_HUDMainMenu.xml");
	}
}

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...