RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Item Script Generator

kmekc

Wanderer
This will create a script file from a targeted item. Very handy if you want to create a set of armor and are too lazy to C/P all the files.

Just create your new set Ingame. Give it a name and a hue. Use "[GenScript <ClassName>" and target the new item and you will receive an email with your newly designed script. Could easily be enhanced for further details or just saving the files to your drive only.

I use it, so my gamemasters can create new stuff without any knowledge of how to program.

P.S.: you have to replace the placeholders for email-adress and mail server credentials

Changes v1.2:
Added Writer for BaseCreature scripts

Changes v1.1
Rewrote for RunUO2
Added built-in support for HDD
better Drop-In-Usage with static variables for configuration
 

Attachments

  • GenScriptv1_2.cs
    9 KB · Views: 336

Nockar

Sorceror
Any idea?

Code:
Errors:
+ Customs/My Scrips/Item Script Generator/GenScript.cs:
    CS0246: Line 14: The type or namespace name 'BaseCommand' could not be found
(are you missing a using directive or an assembly reference?)
    CS0246: Line 23: The type or namespace name 'CommandEventArgs' could not be
found (are you missing a using directive or an assembly reference?)
 

nikkor1132

Sorceror
i get the same error
 
He forgot to adjust his script for RunUO2 :-D
Use this version until he fixes the attached file:

PHP:
using System;
using System.IO;
using System.Net.Mail;
using Server.Targeting;

namespace Server.Commands.Generic
{
    public class GenScriptCommand : BaseCommand
    {
        public static void Initialize()
        {
            Server.Commands.CommandSystem.Register( "genscript",
                                                    AccessLevel.GameMaster,
                                                    new CommandEventHandler( GenScript_OnCommand ) );
        }

        [Usage( "genscript" )]
        [Description( "Generates a script for a targeted item." )]
        public static void GenScript_OnCommand( CommandEventArgs e )
        {
            if ( e.Length != 1 )
            {
                e.Mobile.SendMessage( "Format: genscript <ClassName>" );
                return;
            }

            e.Mobile.Target = new GenScriptTarget( e.GetString( 0 ) );
            e.Mobile.SendMessage( "What item do you want to script?" );
        }

        #region Nested type: GenScriptTarget
        private class GenScriptTarget : Target
        {
            private readonly string ClassName;
            private MemoryStream m_Stream;

            public GenScriptTarget( string classname )
                : base( 15, false, TargetFlags.None )
            {
                ClassName = classname;
            }

            protected override void OnTarget( Mobile from, object targ )
            {
                Item it = targ as Item;

                if ( it == null ) return;

                try
                {
                    m_Stream = new MemoryStream();
                    using ( StreamWriter op = new StreamWriter( m_Stream ) )
                    {
                        op.WriteLine( "using System;" );
                        op.WriteLine( " " );
                        op.WriteLine( "namespace Server.Items" );
                        op.WriteLine( "{" );
                        op.WriteLine( String.Format( "    public class {0} : {1}", ClassName, it.GetType().Name ) );
                        op.WriteLine( "    {" );
                        op.WriteLine( "        [Constructable]" );
                        op.WriteLine( String.Format( "        public {0}() : base( {1} )", ClassName, it.ItemID.ToString() ) );
                        op.WriteLine( "        {" );
                        op.WriteLine( String.Format( "            this.Name = \"{0}\";", it.Name ) );
                        op.WriteLine( String.Format( "            this.Hue = \"{0}\";", it.Hue.ToString() ) );
                        op.WriteLine( "        }" );
                        op.WriteLine( " " );
                        op.WriteLine( String.Format( "        public {0}( Serial serial ) : base( serial )", ClassName ) );
                        op.WriteLine( "        {" );
                        op.WriteLine( "        }" );
                        op.WriteLine( " " );
                        op.WriteLine( "        public override void Serialize( GenericWriter writer )" );
                        op.WriteLine( "        {" );
                        op.WriteLine( "            base.Serialize( writer );" );
                        op.WriteLine( " " );
                        op.WriteLine( "            writer.Write( (int) 0 ); // version" );
                        op.WriteLine( "        }" );
                        op.WriteLine( " " );
                        op.WriteLine( "        public override void Deserialize( GenericReader reader )" );
                        op.WriteLine( "        {" );
                        op.WriteLine( "            base.Deserialize( reader );" );
                        op.WriteLine( " " );
                        op.WriteLine( "            int version = reader.ReadInt();" );
                        op.WriteLine( "        }" );
                        op.WriteLine( "    }" );
                        op.WriteLine( "}" );

                        MailMessage message = new MailMessage( "[email protected]",
                                                               "[email protected]",
                                                               String.Format( "Skript {0}", ClassName ),
                                                               "Blub" );


                        op.Flush();
                        m_Stream.Position = 0;
                        Attachment data = new Attachment( m_Stream, String.Format( "{0}.cs", ClassName ) );
                        message.Attachments.Add( data );

                        //Send the message.
                        SmtpClient client = new SmtpClient( "mymailserver" );
                        // Add credentials if the SMTP server requires them.
                        client.UseDefaultCredentials = false;
                        client.Credentials = new System.Net.NetworkCredential( "[email protected]", "xxxxxxxxxxx" );
                        client.Send( message );
                    }
                    from.SendMessage( "Script generated" );
                }
                catch ( Exception )
                {
                    from.SendMessage( "Scripting failed" );
                }
            }
        }
        #endregion
    }
}
 
It compiles now. But I get Scripting failed.

All the email stuff should be set right.

You can easily check that. Replace:

PHP:
                catch ( Exception )
                {
                    from.SendMessage( "Scripting failed" );
                }

with:

PHP:
                catch( Exception ex )
                {
                    from.SendMessage("Scripting failed");

                    Console.WriteLine( ex.Message );
                    Console.WriteLine( ex.StackTrace );

                }

Now you should see an exception in your console when the script fails.
 

Jeff

Lord
Console.WriteLine( ex ); would be better, since it supplies all information needed for the exception.
 

Nockar

Sorceror
Code:
Errors:
+ Customs/My Scrips/Commands/ItemScriptGenerator.cs:
    CS0103: Line 107: The name 'ex' does not exist in the current context
    CS0103: Line 108: The name 'ex' does not exist in the current context
 

kmekc

Wanderer
Okay rewrote the script for RunUO2 and replaced it in the opening post.

Now has some static variables at the beginning where you can set your Account Credentials etcpp. Also has built in support for saving the script directly to HDD.
 

kmekc

Wanderer
Do you think you could do one for NPC & monsters?

Yes could be done. Would only be an statement if target is Item use this writer and if target is Mobile use the other one.

Edit: Now included in v1.2

Only with some basic informations. If you want to add more. You will have to add it into the Writer functions
 
Console.WriteLine( ex ); would be better, since it supplies all information needed for the exception.

Good to know, thanks. I always did it by hand...

Code:
Errors:
+ Customs/My Scrips/Commands/ItemScriptGenerator.cs:
    CS0103: Line 107: The name 'ex' does not exist in the current context
    CS0103: Line 108: The name 'ex' does not exist in the current context

You didn't replace the first line from the code snippet I showed you.
 

Nockar

Sorceror
Thanks for all the help everyone.

It compiles. Saves the script to my HD.

What it does not do, and I am not sure if its just not setup to do this in the code, when i click a item, it does not add any of the magic properties.

With the way this script is right now does this only give the name & hue or am I missing something?

Code:
using System;

namespace Server.Items
{
    public class test1 : Katana
    {
        [Constructable]
        public test1() : base( 5119 )
        {
            this.Name = "";
            this.Hue = 0;
        }

        public test1( Serial serial ) : base( serial )
        {
        }

        public override void Serialize( GenericWriter writer )
        {
            base.Serialize( writer );

            writer.Write( (int) 0 ); // version
        }

        public override void Deserialize( GenericReader reader )
        {
            base.Deserialize( reader );

            int version = reader.ReadInt();
        }
    }
}
 

kmekc

Wanderer
Code:
op.WriteLine( String.Format( "        public {0}() : base( {1} )", ClassName, it.ItemID.ToString() ) );
                        op.WriteLine( "        {" );
                        op.WriteLine( String.Format( "            this.Name = \"{0}\";", it.Name ) );
                        op.WriteLine( String.Format( "            this.Hue = \"{0}\";", it.Hue.ToString() ) );
                        op.WriteLine( "        }" );

it's just this part. As you can see only Name und Hue. If you wan't more informations in your generated scripts you will have to add it there.

But beware if you want special abilities from weapons. You have to do a cast and check if your item is an armor or weapon

For example this way:

Code:
                        BaseArmor armor = it as BaseArmor;
                        if (armor != null)
                        {
                            op.WriteLine(String.Format(" this.ArmorAttributes.DurabilityBonus = {0}", armor.ArmorAttributes.DurabilityBonus));
                        }

And please remember that this script should help to avoid some C/P action and is not this feature rich. But as you can see, you can easily extend it.
 

michelle78

Sorceror
Could you please give an example of how line 14 should look to make scripts save directly to HD? Thanks

Code:
private static string m_ScriptDirectory = "."; // Directory where to store your newly generated scripts
 
Top