/// <summary>
        /// This routine scans module.ifo for a campaign GUID.  If found, a
        /// campaign.cam file with the same GUID is searched for and its
        /// containing directory is added to the directory resource list.
        /// </summary>
        /// <param name="ModuleIfo">Supplies the module.ifo GFF reader.</param>
        /// <param name="HomeDirectory">Supplies the NWN2 home directory.</param>
        /// <param name="InstallDirectory">Supplies the NWN2 install directory.</param>
        private void AddModuleCampaign(GFFFile ModuleIfo, string HomeDirectory, string InstallDirectory)
        {
            byte[] GUIDData = ModuleIfo.TopLevelStruct.GetVoidDataSafe("Campaign_ID", null);

            if (GUIDData == null || GUIDData.Length != 16)
                return;

            Guid CampaignGUID = new Guid(GUIDData);
            string[] SearchDirs = new string[] { HomeDirectory, InstallDirectory };

            //
            // Attempt to locate a campaign.cam file with the same GUID as the
            // module.
            //

            foreach (string PathName in SearchDirs)
            {
                string CampaignFolder = String.Format("{0}\\Campaigns", PathName);

                if (!Directory.Exists(CampaignFolder))
                    continue;

                foreach (string CampaignDir in Directory.EnumerateDirectories(CampaignFolder))
                {
                    using (DirectoryResourceRepository Repository = new DirectoryResourceRepository(CampaignDir))
                    {
                        Repository.PopulateRepository();

                        IResourceEntry ResEntry = Repository.FindResource(new OEIResRef("campaign"), ResCAM);

                        if (ResEntry == null)
                            continue;

                        GFFFile CampaignCAM = new GFFFile(ResEntry.GetStream(false));

                        GUIDData = CampaignCAM.TopLevelStruct.GetVoidDataSafe("GUID", null);

                        if (GUIDData == null || GUIDData.Length != 16)
                            continue;

                        Guid ID = new Guid(GUIDData);

                        if (!CampaignGUID.Equals(ID))
                            continue;

                        Repositories.Add(new DirectoryResourceRepository(CampaignDir));
                        return;
                    }
                }
            }
        }
Example #2
0
        // NWN2Toolset.NWN2.Views.NWN2BlueprintView.ᐌ(object P_0, EventArgs P_1)
        // yeah whatever. Those idiots were too clever for anybody's good.
        /// <summary>
        /// Saves a specified blueprint to a user-labeled file.
        /// IMPORTANT: Allow only creature-blueprints to be saved!
        /// - NWN2ObjectType.Creature
        /// - resourcetype #2027
        /// @note Check that blueprint is valid before call.
        /// </summary>
        /// <param name="iblueprint">ElectronPanel_.Blueprint or .Instance
        /// converted to a blueprint</param>
        /// <param name="repo">null if Override</param>
        internal static void SaveBlueprintToFile(INWN2Blueprint iblueprint, DirectoryResourceRepository repo = null)
        {
            string fil = iblueprint.Resource.ResRef.Value;
            string ext = BWResourceTypes.GetFileExtension(iblueprint.Resource.ResourceType);

            var sfd = new SaveFileDialog();

            sfd.Title    = "Save blueprint as ...";
            sfd.FileName = fil + "." + ext;               // iblueprint.Resource.FullName
            sfd.Filter   = "blueprints (*." + ext + ")|*." + ext + "|all files (*.*)|*.*";
//			sfd.DefaultExt = ext;

            string dir;

            if (repo != null)
            {
                dir = repo.DirectoryName;
            }
            else
            {
                dir = String.Empty;
            }

            if (!String.IsNullOrEmpty(dir))
            {
                if (Directory.Exists(dir))
                {
                    sfd.InitialDirectory = dir;
                    sfd.RestoreDirectory = true;
                }
            }
            else if (!String.IsNullOrEmpty(CreatureVisualizerPreferences.that.LastSaveDirectory) &&
                     Directory.Exists(CreatureVisualizerPreferences.that.LastSaveDirectory))
            {
                sfd.InitialDirectory = CreatureVisualizerPreferences.that.LastSaveDirectory;
            }
            // else TODO: use NWN2ResourceManager.Instance.UserOverrideDirectory
            // else TODO: get BlueprintLocation dir if exists


            if (sfd.ShowDialog() == DialogResult.OK)
            {
                if (String.IsNullOrEmpty(dir))
                {
                    CreatureVisualizerPreferences.that.LastSaveDirectory = Path.GetDirectoryName(sfd.FileName);
                }


                // NOTE: Add 'AppearanceSEF' back in from the original blueprint
                // since it was removed when the original was duplicated by
                // ElectronPanel_.DuplicateBlueprint().
                (iblueprint as NWN2CreatureTemplate).AppearanceSEF = AppearanceSEF;


                IOEISerializable iserializable = iblueprint;
                if (iserializable != null)
                {
                    iserializable.OEISerialize(sfd.FileName);

                    if (File.Exists(sfd.FileName))                     // test that file exists before proceeding
                    {
                        INWN2BlueprintSet blueprintset;

                        dir = Path.GetDirectoryName(sfd.FileName).ToLower();
                        if (dir == NWN2ToolsetMainForm.App.Module.Repository.DirectoryName.ToLower())
                        {
                            repo         = NWN2ToolsetMainForm.App.Module.Repository;
                            blueprintset = NWN2ToolsetMainForm.App.Module as INWN2BlueprintSet;

                            iblueprint.BlueprintLocation = NWN2BlueprintLocationType.Module;
                        }
                        else if (NWN2CampaignManager.Instance.ActiveCampaign != null &&
                                 dir == NWN2CampaignManager.Instance.ActiveCampaign.Repository.DirectoryName.ToLower())
                        {
                            repo         = NWN2CampaignManager.Instance.ActiveCampaign.Repository;
                            blueprintset = NWN2CampaignManager.Instance.ActiveCampaign as INWN2BlueprintSet;

                            iblueprint.BlueprintLocation = NWN2BlueprintLocationType.Campaign;
                        }
                        else if (IsOverride(dir))
                        {
                            repo         = NWN2ResourceManager.Instance.UserOverrideDirectory;
                            blueprintset = NWN2GlobalBlueprintManager.Instance as INWN2BlueprintSet;

                            iblueprint.BlueprintLocation = NWN2BlueprintLocationType.Global;
                        }
                        else
                        {
                            return;
                        }


                        // so, which should be tested for first: resource or blueprint?
                        // and is there even any point in having one w/out the other?

                        string filelabel = Path.GetFileNameWithoutExtension(sfd.FileName);

                        NWN2BlueprintCollection collection = blueprintset.GetBlueprintCollectionForType(NWN2ObjectType.Creature);

                        INWN2Blueprint extantblueprint = NWN2GlobalBlueprintManager.FindBlueprint(NWN2ObjectType.Creature,
                                                                                                  new OEIResRef(filelabel),
                                                                                                  iblueprint.BlueprintLocation == NWN2BlueprintLocationType.Global,
                                                                                                  iblueprint.BlueprintLocation == NWN2BlueprintLocationType.Module,
                                                                                                  iblueprint.BlueprintLocation == NWN2BlueprintLocationType.Campaign);

                        if (extantblueprint != null && extantblueprint.Resource.Repository is DirectoryResourceRepository) // ie. exclude Data\Templates*.zip
                        {
                            collection.Remove(extantblueprint);                                                            // so, does removing a blueprint also remove its resource? no.
                        }

                        IResourceEntry extantresource = repo.FindResource(new OEIResRef(filelabel), 2027);                         // it's maaaaaagick
                        if (extantresource != null)
                        {
                            repo.Resources.Remove(extantresource);                             // so, does removing a resource also remove its blueprint? no.
                        }


                        iblueprint.Resource = repo.CreateResource(iblueprint.Resource.ResRef, iblueprint.Resource.ResourceType);
                        collection.Add(iblueprint);

                        var viewer = NWN2ToolsetMainForm.App.BlueprintView;
                        var list   = viewer.GetFocusedList();
                        list.Resort();

                        var objects = new object[1] {
                            iblueprint as INWN2Object
                        };
                        viewer.Selection = objects;
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// This routine scans module.ifo for a campaign GUID.  If found, a
        /// campaign.cam file with the same GUID is searched for and its
        /// containing directory is added to the directory resource list.
        /// </summary>
        /// <param name="ModuleIfo">Supplies the module.ifo GFF reader.</param>
        /// <param name="HomeDirectory">Supplies the NWN2 home directory.</param>
        /// <param name="InstallDirectory">Supplies the NWN2 install directory.</param>
        private void AddModuleCampaign(GFFFile ModuleIfo, string HomeDirectory, string InstallDirectory)
        {
            byte[] GUIDData = ModuleIfo.TopLevelStruct.GetVoidDataSafe("Campaign_ID", null);

            if (GUIDData == null || GUIDData.Length != 16)
            {
                return;
            }

            Guid CampaignGUID = new Guid(GUIDData);

            string[] SearchDirs = new string[] { HomeDirectory, InstallDirectory };

            //
            // Attempt to locate a campaign.cam file with the same GUID as the
            // module.
            //

            foreach (string PathName in SearchDirs)
            {
                string CampaignFolder = String.Format("{0}\\Campaigns", PathName);

                if (!Directory.Exists(CampaignFolder))
                {
                    continue;
                }

                foreach (string CampaignDir in Directory.EnumerateDirectories(CampaignFolder))
                {
                    using (DirectoryResourceRepository Repository = new DirectoryResourceRepository(CampaignDir))
                    {
                        Repository.PopulateRepository();

                        IResourceEntry ResEntry = Repository.FindResource(new OEIResRef("campaign"), ResCAM);

                        if (ResEntry == null)
                        {
                            continue;
                        }

                        GFFFile CampaignCAM = new GFFFile(ResEntry.GetStream(false));

                        GUIDData = CampaignCAM.TopLevelStruct.GetVoidDataSafe("GUID", null);

                        if (GUIDData == null || GUIDData.Length != 16)
                        {
                            continue;
                        }

                        Guid ID = new Guid(GUIDData);

                        if (!CampaignGUID.Equals(ID))
                        {
                            continue;
                        }

                        Repositories.Add(new DirectoryResourceRepository(CampaignDir));
                        return;
                    }
                }
            }
        }