Пример #1
0
        public override string SaveFile(string title, string directory, string defaultName, ExtensionFilter[] extensions)
        {
            NativeMethods.OpenFileName sfn = new NativeMethods.OpenFileName();

            string dir = Util.Helper.ValidatePath(directory);
            string defaultExtension = getDefaultExtension(extensions);

            try
            {
                sfn.dlgOwner    = currentWindow;
                sfn.filter      = getFilterFromFileExtensionList(extensions);
                sfn.filterIndex = 1;

                var fileNames = defaultExtension.Equals("*") ? defaultExtension : defaultName + "." + defaultExtension;

                if (!string.IsNullOrEmpty(dir))
                {
                    sfn.initialDir = dir;
                }

#if ENABLE_IL2CPP
                //sfn.file = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(fileNames + Util.Helper.CreateString(" ", MAX_SAVE_FILE_LENGTH - fileNames.Length));
                sfn.file = System.Runtime.InteropServices.Marshal.StringToBSTR(fileNames + Util.Helper.CreateString(" ", MAX_SAVE_FILE_LENGTH - fileNames.Length));
#else
                sfn.file = fileNames + Util.Helper.CreateString(" ", MAX_SAVE_FILE_LENGTH - fileNames.Length);
#endif
                sfn.maxFile = MAX_SAVE_FILE_LENGTH;

                sfn.title  = title;
                sfn.defExt = defaultExtension;
                sfn.flags  = OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;

                sfn.structSize = System.Runtime.InteropServices.Marshal.SizeOf(sfn);

                if (NativeMethods.GetSaveFileName(sfn))
                {
#if ENABLE_IL2CPP
                    //string file = System.Runtime.InteropServices.Marshal.PtrToStringUni(sfn.file);
                    string file    = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(sfn.file);
                    string newFile = Util.Helper.ValidateFile(file);
                    //return newFile.Substring(0, newFile.Length - 1);
                    return(newFile.Substring(0, newFile.Length));
#else
                    string file    = sfn.file;
                    string newFile = Util.Helper.ValidateFile(file);
                    return(newFile);
#endif
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex);
            }

            return(string.Empty);
        }
Пример #2
0
        public override string SaveFile(string title, string directory, string defaultName, params ExtensionFilter[] extensions)
        {
            NativeMethods.OpenFileName sfn = new NativeMethods.OpenFileName();

            string dir = Util.Helper.ValidatePath(directory);
            string defaultExtension = getDefaultExtension(extensions);

            try
            {
                string fileName = defaultExtension.Equals("*") ? defaultExtension : $"{defaultName}.{defaultExtension}";

                sfn.dlgOwner    = currentWindow;
                sfn.filter      = getFilterFromFileExtensionList(extensions);
                sfn.filterIndex = 1;

                if (!string.IsNullOrEmpty(dir))
                {
                    sfn.initialDir = dir;
                }

                //sfn.file = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(fileNames + Util.Helper.CreateString(" ", MAX_SAVE_FILE_LENGTH - fileNames.Length));
                sfn.file    = System.Runtime.InteropServices.Marshal.StringToBSTR(fileName + Util.Helper.CreateString(" ", MAX_SAVE_FILE_LENGTH - fileName.Length));
                sfn.maxFile = MAX_SAVE_FILE_LENGTH;
                sfn.title   = title;
                sfn.defExt  = defaultExtension;
                sfn.flags   = (int)FOS.FOS_NOCHANGEDIR | (int)FOS.FOS_PATHMUSTEXIST | (FileBrowser.Instance.AskOverwriteFile ? (int)FOS.FOS_OVERWRITEPROMPT : 0);

                sfn.structSize = System.Runtime.InteropServices.Marshal.SizeOf(sfn);

                if (NativeMethods.GetSaveFileName(ref sfn))
                {
                    //string file = System.Runtime.InteropServices.Marshal.PtrToStringUni(sfn.file);
                    string file    = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(sfn.file);
                    string newFile = Util.Helper.ValidateFile(file);
                    //return newFile.Substring(0, newFile.Length - 1);
                    return(newFile.Substring(0, newFile.Length));
                }
            }
            catch (Exception ex)
            {
                Debug.LogError($"Save file dialog threw an error: {ex}");
            }

            return(string.Empty);
        }
Пример #3
0
        public override string[] OpenFiles(string title, string directory, string defaultName, bool multiselect, params ExtensionFilter[] extensions)
        {
            NativeMethods.OpenFileName ofn = new NativeMethods.OpenFileName();
            string dir = Util.Helper.ValidatePath(directory);

            try
            {
                string fileName = defaultName ?? "";

                ofn.dlgOwner    = currentWindow;
                ofn.filter      = getFilterFromFileExtensionList(extensions);
                ofn.filterIndex = 1;

                if (!string.IsNullOrEmpty(dir))
                {
                    ofn.initialDir = dir;
                }

                //ofn.file = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(Util.Helper.CreateString(" ", MAX_OPEN_FILE_LENGTH));
                ofn.file = System.Runtime.InteropServices.Marshal.StringToBSTR(fileName + Util.Helper.CreateString(" ", MAX_OPEN_FILE_LENGTH - fileName.Length));

                ofn.maxFile = MAX_OPEN_FILE_LENGTH;
                ofn.title   = title;
                ofn.flags   = (int)FOS.FOS_NOCHANGEDIR | (int)FOS.FOS_FILEMUSTEXIST | (int)FOS.FOS_PATHMUSTEXIST | (multiselect ? (int)FOS.FOS_ALLOWMULTISELECT | OFN_EXPLORER : 0x00000000);

                ofn.structSize = System.Runtime.InteropServices.Marshal.SizeOf(ofn);

                if (NativeMethods.GetOpenFileName(ref ofn))
                {
                    //string file = System.Runtime.InteropServices.Marshal.PtrToStringUni(ofn.file);
                    string file = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ofn.file);

                    if (multiselect)
                    {
                        string[] files = file.Split(nullChar, StringSplitOptions.RemoveEmptyEntries);

                        if (files.Length > 2)
                        {
                            System.Collections.Generic.List <string> selectedFilesList =
                                new System.Collections.Generic.List <string>();

                            for (int ii = 1; ii < files.Length - 1; ii++)
                            {
                                string resultFile = Util.Helper.ValidateFile(files[0] + '\\' + files[ii]);
                                selectedFilesList.Add(resultFile);
                            }

                            return(selectedFilesList.ToArray());
                        }
                    }

                    return(new[] { Util.Helper.ValidateFile(file) });
                }
            }
            catch (Exception ex)
            {
                Debug.LogError($"Open file dialog threw an error: {ex}");
            }

            return(new string[0]);
        }